Jump to content

Shell script that emulates the tree command in Linux

- - - - -

  • Please log in to reply
7 replies to this topic

#1
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
#!/bin/bash

# This shell program prints a tree diagram of
# the files in a directory, using recursion.
# It is an emulation of the Linux tree command,
# which I don't have on my Mac.

olddir=$PWD;
color="off";
for option in "$@"
do
    case $option in
        --color ) color="on";
              shift;;
        -*    ) shift;;
        *       )      ;;
    esac
done
declare -i stackheight=0;
# stackheight determines the directory depth.
function listfiles {
    cd "$1";
    for file in *
    do
        for ((i=0; $i < $stackheight; i++))
        do
            printf "\t";
        done
        # indentation shows the depth of the file
        if [ -d "$file" ]
        then
            if [ $color == "on" ]
            then
                printf "\e[34m";
                # display directory names in blue
            fi
        fi
        printf "$file\e[0m\n";
        if [ -d "$file" ]
        then
            stackheight=$stackheight+1;
            # more indentation
            listfiles "$file";
            # recursive listing of files
            cd ..;
        fi
    done
    let stackheight=$stackheight-1;
    # less indentation
}
listfiles "$1";
cd $olddir;
unset i color stackheight;

Edited by DarkLordoftheMonkeys, 10 March 2010 - 05:32 AM.

Life's too short to be cool. Be a nerd.

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Nice! +rep. I think this'll fix your problem with the spaces:

Instead of:
for file in *
do
    ...
done
Try this:
declare -a  FILELIST
FILELIST="*"

for file in ${FILELIST[@]}
do
    ...
done
Can't test it right now, but you can try it.

EDIT: Change all your if [ ... ] to if [[ ... ]]; and it'll work for spaces.
sudo rm -rf /

#3
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
Doesn't matter. I already fixed it weeks ago. Thanks though.
Life's too short to be cool. Be a nerd.

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Ah, well you should've posted the solution then. :)
sudo rm -rf /

#5
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
I've just edited the original post with the script that works. All I did was put quotation marks around the arguments, and use "$@" instead of $*.
Life's too short to be cool. Be a nerd.

#6
AlanW

AlanW

    Newbie

  • Members
  • Pip
  • 2 posts
Hi DarkLordoftheMonkeys,

Thank you for a great and useful script. I know it's been a while since you posted this, and hopefully you check the forum from time to time, but is there a way to have the files print with their dates as well? I'm not a coder of your caliber and tried inserting 'ls' into your code- but obviously, no go.

My end goal is to list the file tree and pipe it to a csv file:

Dir
....File.1 20120202
....File.2 20120205
....File.3 20120212
....Dir2
.........File.4 20100101
.........File.5 20100525
...
Any help will be greatly appreciated!

Alan.

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Please don't post on threads more than a month old; start your own new one instead. The people originally involved have probably moved on to other things and newer active threads get pushed down in the list.

Thanks!
sudo rm -rf /

#8
AlanW

AlanW

    Newbie

  • Members
  • Pip
  • 2 posts
Well, OK.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users