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-320198ce6fd}   
   |---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/sh ####################################################### # UNIX TREE # # Version: 2.3 # # File: ~/apps/tree/tree.sh # # By Dem Pilafian # # # # 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 .. # # # # Public Domain Software -- Free to Use as You Like # # http://www.centerkey.com/tree # ####################################################### 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
Message:
Name:
E-Mail:

Powered by PERFECT

All the fields are optional.  However, if you want a response, make sure to provide your e-mail 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

Post Bookmark   Post Bookmark   Post Bookmark   Post Bookmark   Post Bookmark
Colors
Google
Tony Lauro's Colorized Version
"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
"...you could replace 'ls -R | grep' with 'find . -type d'."
Anonymous, February 15, 2006
More convenient, but not as universal.  — 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