Unix Tree / Linux Tree

Display Structure of Directory Hierarchy

One-Line Shell Script

ls, grep, and sed

Quiz
Quick, what does the following Unix/Linux command do?

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'

If you said, "Well, that's obvious; it shows a graphical representation of the current sub-directories.", you'd be correct.

Example

You can use the command to create a program called tree that would work something like the following:
dem@ubuntu:~$ tree .local

/home/dem/.local
   |-share
   |---applications
   |---desktop-directories
   
dem@ubuntu:~$ cd apps/firefox
dem@ubuntu:~$ tree

/home/dem/apps/firefox
   |-chrome
   |---icons
   |-----default
   |-components
   |-defaults
   |---autoconfig
   |---pref
   |---profile
   |-----chrome
   |-extensions
   |---{972ce4c6-7e08-474-a285-320298ce6fd}
   |---inspector~mozilla.org
   |-----chrome
   |-----components
   |-----defaults
   |-------preferences
   |---talkback~mozilla.org
   |-----components
   |-------talkback
   |-greprefs
   |-icons
   |-plugins
   |-res
   |---dtd
   |---entityTables
   |---fonts
   |---html
   |-searchplugins
   |-updates
   |---0

Code
Here's the command ready-to-go in a shell script:

tree.sh


         #!/bin/bash
#######################################################
#  UNIX TREE                                          #
#  Version: 2.5                                       #
#  File: ~/apps/tree/tree.sh                          #
#                                                     #
#  Displays structure of directory hierarchy          #
#  -------------------------------------------------  #
#  This tiny script uses "ls", "grep", and "sed"      #
#  in a single command to show the nesting of         #
#  sub-directories.  The setup command for PATH       #
#  works with the Bash shell (the Mac OS X default).  #
#                                                     #
#  Setup:                                             #
#     $ cd ~/apps/tree                                #
#     $ chmod u+x tree.sh                             #
#     $ ln -s ~/apps/tree/tree.sh ~/bin/tree          #
#     $ echo "PATH=~/bin:\${PATH}" >> ~/.profile      #
#                                                     #
#  Usage:                                             #
#     $ tree [directory]                              #
#                                                     #
#  Examples:                                          #
#     $ tree                                          #
#     $ tree /etc/opt                                 #
#     $ tree ..                                       #
#                                                     #
#  WTFPL - Free to use as you like                    #
#  centerkey.com/tree - By Dem Pilafian               #
#######################################################

echo
if [ "$1" != "" ]  #if parameter exists, use as base folder
   then cd "$1"
   fi
pwd
ls -R | grep ":$" |   \
   sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
# 1st sed: remove colons
# 2nd sed: replace higher level folder names with dashes
# 3rd sed: indent graph three spaces
# 4th sed: replace first dash with a vertical bar
if [ $(ls -F -1 | grep "/" | wc -l) = 0 ]   # check if no folders
   then echo "   --> no sub-directories"
   fi
echo
exit
      


view/download

That's it.

Feedback
Got any questions or comments?

Send us a message

Powered by PERFECT

All the fields are optional.  However, if you want a response, make sure to provide your email address.

Note:  On some systems there is a directory search utility called find which returns a tree-like list.  It's very handy, but it is not universal like ls, grep, and sed.

Random

"Thank you very much man for this tool ;)"
P., January 30, 2010
"Was literally damn useful..Gr8 work"
A., December 2, 2009
"The script works very well.  It is exactly what I was looking for."
E.A.H., November 26, 2009
"nice one .it helped me alot.thnx a lot.Cheers:-)"
M., September 19, 2009
"small and powerful as a bash script should be!"
M., July 23, 2009
"Very handfull!  You just saved me a lot of typing!"
I.M., June 23, 2009
"'Those who command the shell shall inherit the earth..' - Nice, elegant work!"
J.C., May 6, 2009
"That's awesome man!"
M., March 15, 2009
"Beautiful & powerful !"
T., March 13, 2009
"Awesome!  Thanks for sharing!"
N., February 9, 2009
"Wonderful script, especially the more informative comments, source and document in a single file.good"
R.U.V., December 17, 2008
"simple and nice.."
G., November 3, 2008
"I've challenged myself to come up with a solution based on this script that would not only output directories, but also the files in these directories.  I've tried for several hours but I haven't been able to come up with a solution...  Will anybody take the challenge and come up with a working solution ?"
D.J., October 8, 2008
"You might want to note that the above script requires the "GNU" versions of grep/sed."
F., August 30, 2008
"This is great!  I was already using a cut-down version to list directories, but I was frustrated that it didn't look very tidy!  This is exactly what I needed and am now using at every client site that I visit! :)"
J.L., August 21, 2008
"Thank you much!!  I changed double-dash to dash-space because I like that better.  Your comments in the code enabled me to make the change easily even though I don't know sed.  Thank you for helping me learn Linux!!"
R.R., August 7, 2008
"That's such a sweet program, thanks!"
J.M., August 7 2008
More...