How To Create Directory Trees In ASCII

I am in the middle of writing a blog post and I needed to represent a directory structure in a quick and easy way. Here’s the command I used for that:

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

On Windows I used Cygwin to run this *nix command. And what I got was pretty satisfactory:

   .
   |-ear
   |-ejbs
   |---src
   |-----main
   |-------resources
   |---------META-INF
   |-primary-source
   |-projects
   |---logging
   |-servlets
   |---servlet
   |-----src
   |-------main
   |---------webapp
   |-----------WEB-INF
   |-src
   |---main
   |-----resources

Let me know if you have a better or quicker or nicer way of doing the same thing.

UPDATE: As suggested by Derek, you can use the “tree /a” command on Windows, for even a prettier output:

+---ear
+---ejbs
|   \---src
|       \---main
|           \---resources
|               \---META-INF
+---primary-source
+---projects
|   \---logging
+---servlets
|   \---servlet
|       \---src
|           \---main
|               \---webapp
|                   \---WEB-INF
\---src
    \---main
        \---resources

, ,

  1. #1 by Dale Wyttenbach (@wytten) on September 1, 2012 - 4:53 am

    Nice!

  2. #2 by Derek Noonan (@noonand) on September 3, 2012 - 4:00 pm

    On Windows there’s no need to install Cygwin, just type “tree /a” from the top-level location you wish to build the tree from. As with most commands you can redirect the output to a text file viz. tree /a > mytree.txt

  3. #3 by Yagiz Erkan on September 3, 2012 - 4:07 pm

    Thanks Derek. It’s a nicer output as well. I’ll update my post with that info.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: