Tree Utility

written by Matthias Arndt<matthiasarndt@gmx.net>

What is this?

This is a new version of the bash based tree utility which was published in the Linux Gazette about 2 years ago.

I've added the feature to display the files inside the directories.
This tool displays the whole directory tree below the PWD. You may supply an alternate starting directory on the command line.

This is a bash script so it is not very fast.
But obviously, it does its job.


Source code of the Tree utility

#!/bin/sh
#         @(#) tree      2.0 04/08/00
#                                         original version by Jordi Sanfeliu
#                                         email: mikaku@arrakis.es
#
#         Initial version:  1.0  30/11/95
#         Next version   :  1.1  24/02/97   Now, with symbolic links
#         Patch by       :  Ian Kjos, to support unsearchable dirs
#                           email: beth13@mail.utexas.edu
#         File support   :  by Matthias Arndt  
#                           email: matthiasarndt@gmx.net                           
#
#         script published by Linux Gazette (http://www.linuxgazette.com)
#
#         Tree is a tool for viewing the directory tree (obvious :-) )
#
search () {
	for dir in `echo *`
	do
		if [ -d $dir ] ; then
			zz=0
			while [ $zz != $deep ]
			do
				echo -n "|   "
				zz=`expr $zz + 1`
			done
			if test -L $dir ; then
				echo "+---$dir" `ls -l $dir | sed 's/^.*'$dir' //'`
			else
				echo "+---$dir/"
				if cd $dir ; then
					deep=`expr $deep + 1`
					search    # with recursivity ;-)
					numdirs=`expr $numdirs + 1`
				fi
			fi
		else
                        # support to display the files here...		
			if [ -f $dir ] ; then
				zz=0
				while [ $zz != $deep ]
				do
					echo -n "|   "
					zz=`expr $zz + 1`
				done
				echo "+---$dir"
			fi
		fi
	done
	cd ..
	if [ $deep ] ; then
		swfi=1
	fi
	deep=`expr $deep - 1`
}

# - Main -
# save the current directory to the stack
# the old version would put you inside the dir you gave
#on the command line
pushd $PWD >/dev/null
if [ $# = 0 ] ; then
	cd `pwd`
else
	if test $1 = "-?"
	then
		echo "Usage: $0 [optional starting dir]"
		exit 1
	fi
	cd $1
fi
echo "Initial directory = `pwd`"
swfi=0
deep=0
numdirs=0
zz=0

while [ $swfi != 1 ]
do
	search
done
echo "Total directories = $numdirs"
# restore the directory
popd >/dev/null

To use the script, just cut and paste the code to your favourite editor. Call it tree and make the script executable using chmod u+x.
I suggest copying it to /usr/local/bin as root and do a chmod +x on it to make it available to all users on your system.
As in all unix tools, the output goes straight to the stdout. This means you can use I/O redirection to capture the resulting tree to a file.


Archived distribution

A tar-ball (.tar.gz file) is available on my website http://www.geocities.com/asmsoftware/.
Maybe I'll create *.rpm or even *.deb files from it.


Licence

As on all of my releases of software for Linux, the GNU General Public Licence (GPL) applies to this utility.