Unix Tree / Linux Tree
Display Structure of Directory Hierarchy
One-Line Shell Script
ls, grep, and sed
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.
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
|
Here's the command ready-to-go in a shell script:
tree.sh
#!/bin/sh
#######################################################
# UNIX TREE #
# Version: 2.4 #
# 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 .. #
# #
# Public Domain Software -- 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.
Got any questions or comments?
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.
"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...
"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