Displaying a text-based file and folder tree on the command line
If you need to see a visual layout of files and directories from the current directory in a tree, you can do so by piping the output from a recursive “ls” command into a “grep” search, then piping those results into “sed” replacements to build the tree view.
Open a new Terminal window, switch to the directory you’d like to list, then enter this command:
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
Explanation of the commands:
ls -R – list all files recursively
grep “:$” – find all lines ending in “:” (a colon)
sed -e ’s/:$//’… – remove all trailing “:” (colons), replace “-” (dashes) with “|” (pipes), and replace “/” (forward slashes) with “–” (double dashes)
Example output:
|-Outlook Plugins |-QuickLook Plugins |-Quicktime Codecs |---Perian.component |-----Contents |-------MacOS |-------Resources |---------English.lproj |-----------main.nib |---------French.lproj |-----------main.nib |---XviD_Codec-r58 (Intel).component |-----Contents |-------MacOS |-------Resources |---------English.lproj |---------XviD_Codec.nib |---------XviD_Export.nib
Related posts:
- Reusing commands with different arguments on a Bash command line
- Checking directory sizes on a Bash command line
- Editing, Validating and Querying XML with the XMLStarlet command line utility
- Mac OS X Quick Tip: Using Spotlight to search from the command line
- Ubuntu Linux Quick Tip – Mount a Samba (Windows) file share to a folder



29 Jul 2009 








author
Alternativley, use the ‘tree’ command. e.g.
You can get it from your favourite repo, e.g. apt-get install tree
Great, thanks Dominic!