Unix/Linux Folder Tree

Display structure of directory hierarchy

One-line Bash shell script

Just 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/^/   |/"
         

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

Example

You can use the command to create a program called tree that would work something like the following:


            $ tree /etc/apache2
            
            /etc/apache2
               |--extra
               |--original
               |----extra
               |--other
               |--users
            
            $ tree /etc/apache2/other
            
            /etc/apache2/other
               --> no subfolders
            
            $ cd ~/apps/firefox
            $ 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
            
            $ which tree
            /usr/local/bin/tree
         

Code

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

tree.sh


            #!/bin/bash
#####################################################
# Unix/Linux Folder Tree                            #
# Version: 2.7                                      #
# File: ~/apps/tree/tree.sh                         #
#                                                   #
# Displays structure of folder hierarchy            #
# ------------------------------------------------- #
# This tiny script uses "ls", "grep", and "sed"     #
# in a single command to show the nesting of        #
# subfolders.                                       #
#                                                   #
# Setup:                                            #
#    $ cd ~/apps/tree                               #
#    $ chmod +x tree.sh                             #
#    $ ln -s tree.sh /usr/local/bin/tree            #
#    $ which tree                                   #
#                                                   #
# Usage:                                            #
#    $ tree [FOLDER]                                #
#                                                   #
# Examples:                                         #
#    $ tree                                         #
#    $ tree /etc/apache2                            #
#    $ tree ..                                      #
#                                                   #
# WTFPL ~ https://centerkey.com/tree ~ Dem Pilafian #
#####################################################

echo
test -z "$1" || cd "$1"  #if parameter exists, use as base folder
pwd
ls -R | grep "^[.]/" | sed -e "s/:$//" -e "s/[^/]*[/]/--/g" -e "s/^/   |/"
# grep:    select folders (filter out files)
# 1st sed: remove trailing colon
# 2nd sed: replace higher level folder names with dashes
# 3rd sed: indent graph and add leading vertical bar
topFolders=$(ls -F -1 | grep "/" | wc -l)
test $topFolders -ne 0 || echo "   --> no subfolders"
echo
         

Download script: tree.sh

Or just install using this little script: install-tree.sh.command

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

That's it.

Feedback

Got a question or comment?

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.

Random

"Thank you for perfect script! :)  Very need for this at AIX 4.3!" A.B., July 24, 2013
"Wicked!" I., March 14, 2013
"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
More...
"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
"A bug fix: in your script, the initial 'cd' command fails if the argument contains any spaces.  Change the 'then cd $1' to 'then cd "$1"' and all will be sweetness and light." D.G., February 2, 2008 Fix put into version 2.2
"A nice tool.  Thanks." D.G., February 2, 2008
"thank You  ;)" A., November 14, 2007
"Excellent!  Works as it should!" J.J., October 11, 2007
"Very excellent" J.M., June 29, 2007
"Thank u very much.  Was really amazed by the power of sed" P.M., June 23, 2007
"COOL!  Man" J.T., February 1, 2007
"This fit nicely into my Cygwin setup.  :)" B., November 29, 2006
"I googled 'linux show directory structure' and this marvellous script popped up first!  Many thanks." O.B., October 4, 2006
"I think a little touch such: ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--|/g' -e 's/^/ /' -e 's/-/|/' makes it easy reading.  Thanks." D.K., September 12, 2006
"Nice one!!  Short and geeky..." A., August 31, 2006
"Great script.  Just what I was loking for.  Thanks" Y., August 15, 2006
"genius little script!  helps a lot when teaching and you have to show the directory structure of our progams." B.S., July 4, 2006
"Excellent !!  Thanks" G.T., May 2, 2006
"Thanks for this great script.  Simple and powerful!" F.W., April 5, 2006
"great script, macosx lacks the tree command, and you can't put fink on every mac..  just do what it's told.  Nice !" X., March 28, 2006
"Thanks for the great script.  Our users who switched to Unix from VMS will love this.  ls -R | grep ":$" | \ may work better in case files have : in their names." S., February 15, 2006 Great suggestion...  adopted in version 2.1.  — Dem
"tnx for the script, I like it and now is on my Solaris 10." D.S., January 20, 2006
"Wow!  A nice demonstration of the power of regular expressions and the infinite patience of the author." C.M., December 30, 2005
"Clearly an improvement of the ls command." Z.E., December 30, 2005
"That is pretty cool dude!" S.K., December 29, 2005