How to generate an ASCII representation of a Unix file hierarchy?

all. I'm looking for a quick and dirty way to generate some diagrams of some directories that have almost, but not exactly, the same hierarchy, so I can show them around at a meeting and we can decide which flavor we like best. I'm not interested in the "leaf" nodes, just the directories.

The catch: I don't want to mess with X. This is a server system I deal with entirely through SSH. So I'm looking for something that will do ASCII layout, maybe with simple pipes-and-hyphens for lines or something.

Does anyone know of such a utility? I'm sure I could write something myself, but it's such a fiddly little sort of project, with handling spacing and layout and such; I'd really like to discover that someone's done it for me. Alas, Google doesn't seem to know of such a thing...or if it does, it's hidden beneath heaps of excellent visual explications of the standard general Unix file hierarchy. Thanks!


I would use tree.

$ tree -d /usr|head -n 12
/usr
|-- X11R6
|   `-- lib
|       `-- X11
|           `-- wily
|-- bin
|   `-- X11 -> .
|-- games
|-- i586-mingw32msvc
|   |-- bin
|   |-- include
|   |   |-- GL

If you don't have tree you could use this linux/unix command:

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

You can also make a shell script see details here.

Explanation for the above command:

ls -R list all directories, sub-directories,

Explanation ls -R list all the file and directories recursively

ex:

./sys/devices/platform/ag71xx.0/net/eth0:
addr_assign_type  device            iflink            speed


./sys/devices/platform/ag71xx.0/net/eth0/queues:
tx-0

grep ":$" filters only the files that have : before line end, thus remains, something like.

./sys/devices/platform/ag71xx.0/net/eth0:
./sys/devices/platform/ag71xx.0/net/eth0/queues:

Then a series of multiple command are passed using -e switch to sed

's/:$//' strips all the trailin :

's/[^-][^\/]*\//--/g' leaves only what is between / - and replace each with --

the rest two command add a few spaces and a |

The result is something like:

   |-----------eth0
   |-------------queues