hi guys i'm trying to make a script that prints out a specified directory as some sort of tree, it prints out ok but i need it to print out every subdirectory within the directory but i'm not sure how to do that, this only goes in one or two levels deep but i need everythign inside that directory.. i'm guessing i'll need some sort of recursive algorithim?
dir=$1
for FILE in $dir/*
do
if [ -d "$FILE" ]; then
echo "|-- `echo $FILE | cut -f2 -d/`"
for FILE in $dir/$FILE/*
do
echo "| |-- `echo $FILE | cut -f2 -d/`"
done
echo "|-- `echo $FILE | cut -f2 -d/`"
fi
done
vino
August 18, 2006, 3:03am
2
Why reinvent the wheel ?
Ygor has posted a handy one-liner in Tree with UNIX
I have modified it a little and you can find it here
Ygor
August 18, 2006, 3:40am
3
Hi Vino,
Your modified one-liner is missing the "-type d" option in find, so that the directory tree also shows files. This is slightly different to what was required.
Originally from http://www.tek-tips.com/faqs.cfm?fid=3705
vino
August 18, 2006, 3:54am
4
Ygor, much appreciated !
Thanks.
find . -type d -print | sed -e 's;[^/]*/;|--;g;s;--|; |;g'
works wonderfully thanks!
now here's how it appears:
.
|--lower.sh
|--guess.sh
|--dir
| |--one-file
| |--one-file.2
| |--bla
| |--bla2
| | |--one-file.2
| | |--ba;a;a
| | |--three-file
| | |--Copy of ba;a;a
| | | |--ba;a;a
|--superlower.sh
|--example.sh
how can i make it so that it appears like this:
.
|--lower.sh
|--guess.sh
|--dir
| |--one-file
| |--one-file.2
| |--bla
| |--bla2
| |--one-file.2
| |--ba;a;a
| |--three-file
| |--Copy of ba;a;a
| |--ba;a;a
|--superlower.sh
|--example.sh
so no lines show up if theres nothing to connect to
I've decided to stick with my own code cos it needs to have loops and functions and the oneliner code (as good as it is) doesnt
I'm not familiar with use of method functions so I wrote this but it won't work, it gives me an error saying
printDir: command not found
Here's my code
if [ "$2" != "" -o "$1" == "--help" ]; then
echo "Usage: ./viztree.sh [directory]"
else
dir=$1
printDir $dir
fi
printDir () {
dir=$1
for FILE in $dir/*
do
if [ -d "$FILE" ]; then
echo "|-- `echo $FILE | cut -f2 -d/`"
echo '$dir' = $dir/$FILE
printDir $dir
else
echo "|-- `echo $FILE | cut -f2 -d/`"
fi
done
}
I was thinking that to call methods you pass an argument say methodName arg1 or something? am i wrong?
vino
August 19, 2006, 1:38am
7
You are invoking a method which is defined later in the script. Move the method definition to before its invocation.
printDir () {
dir=$1
for FILE in $dir/*
do
if [ -d "$FILE" ]; then
echo "|-- `echo $FILE | cut -f2 -d/`"
echo '$dir' = $dir/$FILE
printDir $dir
else
echo "|-- `echo $FILE | cut -f2 -d/`"
fi
done
}
if [ "$2" != "" -o "$1" == "--help" ]; then
echo "Usage: ./viztree.sh [directory]"
else
dir=$1
printDir $dir
fi
Thanks! it runs now, but the results are not what I expected :S am i passing the arguments in the wrong way? it just prints out first level and second levels deep in subdirectory and keeps printing it out without stopping
i've decided to stick with this but I still need the answer to this qeustion
find . -type d -print | sed -e 's;[^/]*/;|--;g;s;--|; |;g'
works wonderfully thanks!
now here's how it appears:
.
|--lower.sh
|--guess.sh
|--dir
| |--one-file
| |--one-file.2
| |--bla
| |--bla2
| | |--one-file.2
| | |--ba;a;a
| | |--three-file
| | |--Copy of ba;a;a
| | | |--ba;a;a
|--superlower.sh
|--example.sh
how can i make it so that it appears like this:
.
|--lower.sh
|--guess.sh
|--dir
| |--one-file
| |--one-file.2
| |--bla
| |--bla2
| |--one-file.2
| |--ba;a;a
| |--three-file
| |--Copy of ba;a;a
| |--ba;a;a
|--superlower.sh
|--example.sh
so no lines show up if theres nothing to connect to
leosag
August 20, 2008, 12:40am
10
can someone help with the code
it only goes thru one level how can i make it read thru the subdirectories as well