Directory tree10 August 2009

A handy Bash script to display a tree view of a directory, adapted from http://www.centerkey.com/tree. This version omits .svn and .git directories, and uses the find utility.

echo
if [ "$1" != "" ]  #if parameter exists, use as base folder
   then cd "$1"
   fi
pwd
find . \! \( -path "*.svn*" -or -path "*.git*" \) -type d | \
   sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
if [ `ls -F -1 | grep "/" | wc -l` = 0 ]
   then echo "   -> no sub-directories"
   fi
echo
exit
Example use:
[cgansen@Crystal-Frontier ~]$ tree projects/self.d-struct.org/wp-admin/

/Users/cgansen/projects/self.d-struct.org/wp-admin
   .
   |-css
   |-images
   |-import
   |-includes
   |-js

[cgansen@Crystal-Frontier ~]$

Want to talk about this a bit more? Send a tweet to @cgansen or email me at cgansen@gmail.com.