printing directories

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

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

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

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?

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 :frowning:

can someone help with the code
it only goes thru one level how can i make it read thru the subdirectories as well