Display a tree-like structural view of any directory

Feb 15, '06 06:31:00AM

Contributed by: raimondi

First of all this is not my code, I found it on this page via Google, while looking for something that would work like the 'tree' command in Windows.

This guy found a way to do so, using just ls, grep and sed; pretty neat! Here's the command:

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

The author of this useful command even provides a downloadable shell script for the code, and explains how to make a link to ~/bin/tree for those less Unix savvy users. Enjoy!

[robg adds: First, I apologize for the scrolling box, but I didn't want to take any chances on changing the format of the command by inserting a line break, and it was otherwise too wide for display. I'll be the first to admit I don't understand anything in that command past the colon-replacement bit, but it does work quite nicely. Here's a sample of the output, using the shell script from the linked page:
$ tree /Applications | more
   |-Address Book.app
You may wish to add | more at the end to page through the output. Or try using > ~/Desktop/mytree.txt at the end to dump the output to a file. I do not, however, recommend doing that with a huge directory such as Applications, unless you have some time to wait. I tried it, and after a few minutes of waiting, wound up with a 1.1mb text file containing 37,000+ rows!]

Comments (33)

Mac OS X Hints