My professor sent us this little one liner (ok, I had to format it to 2 lines to fit in this blog. You know what I mean) which prints out the current directory tree:
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' \ -e 's/^/ /' -e 's/-/|/'
What’s going on here?
ls -R— list files and directories recursivelygrep ":$"— find lines with : at the end (so only the directories)sed -e— evaluate expressions on the liness/:$//— remove ‘:’ at the end of the lines/[^-][^\/]*\//--/g— replaces text between / / lines (parent directories) with — , globally on each lines/^/ /— add space at the beginning of the liness/-/|/— replace first – of the line with |
I reduced this using the following command. The most notable difference is that I use find instead of ls, which results in also viewing .hidden directories. I’m not sure which command is faster.
find ./ -type d | sed -e 's/[^-][^\/]*\//--/g;s/--/ |-/'
Both commands result in a formatted directory listing, demonstrated below:
|-sitetransfer |---redacteddomain.com |-----cache |-----templates |-------skidoo_too |---------images |-----------_vti_cnf |---------css |-----------_vti_cnf |---------js |-----------scriptaculous |-------------src |-------------lib |---------admin_templates

3 Trackbacks
You can leave a trackback using this URL: http://systembash.com/content/one-line-linux-command-to-print-out-directory-tree-listing/trackback/
New Post: One Line Linux Command to Print Out Directory Tree Listing http://bit.ly/bN4CIc
One Line Linux Command to Print Out Directory Tree Listing http://ff.im/-gs2zG
Nice One Dave! RT @ddrager: One Line Linux Command to Print Out Directory Tree Listing http://bit.ly/bN4CIc
2 Comments
New Post: One Line Linux Command to Print Out Directory Tree Listing http://bit.ly/bN4CIc
This comment was originally posted on Twitter
One Line Linux Command to Print Out Directory Tree Listing http://ff.im/-gs2zG
This comment was originally posted on Twitter