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 recursively
  • grep ":$" — find lines with : at the end (so only the directories)
  • sed -e — evaluate expressions on the lines
  • s/:$// — remove ‘:’ at the end of the line
  • s/[^-][^\/]*\//--/g — replaces text between / / lines (parent directories) with — , globally on each line
  • s/^/ / — add space at the beginning of the lines
  • s/-/|/ — 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
5 comments
  1. Pingback: Dave Drager
  2. Pingback: GeekLad
  3. Pingback: Karl Gechlik

Comments are closed.

You May Also Like

How to Turn PHP into an RPM The Easy Way with FPM

So you have a custom PHP binary that you wish to distribute…

How to Install SNMP on Tomato Router Firmware and Graph Traffic with Cacti

You’ve flashed your old WRT54G or other vanilla router with the Tomato…

PowerDNS flexible, fast DNS Server

I’ve recently been testing/installing PowerDNS for a web hosting provider. Man am…

Clearing spamassassin BAYES filter tokens

I recently had a problem where my Spamassassin install started thinking that…