The Tree Command

Today I'm working on a little tutorial (about writing RESTful services, for this site) and I used the tree command to illustrate the file and directory layout of the project. I love this little command and use it frequently, but it isn't very well known so here's a quick example.

As with most linux command line utilities, it does only one thing and it does it very well. In this case, it shows the file structure. For my work-in-progress project (which handily only has a few files so has fairly short output), it does this:

$ tree .
.
├── controllers
│   ├── MyController.php
│   └── UsersController.php
├── index.php
├── models
└── views

3 directories, 3 files

The . that was passed here is the name of the directory to generate the tree for; the dot means to use the current directory. You can also pass switches limiting the depth of the listing, showing full paths to each file or directory, and omitting the indentations - as well as a large number of other options that seem less useful!

I use the tree command to put into blog posts, talk slides, and so on - I think it's clear and easy to read (especially because the toolchain I'm using for slides at the moment make most screenshots look decidedly chewed!), and also just to remind myself where all the various pieces of a project can be found.

7 thoughts on “The Tree Command

  1. This is a really useful command to have in your toolbox.

    For Debian based Linux distros you can easily install it using Apt:
    $ sudo aptitude install tree

    FYI you can save two key strokes by just typing 'tree'. ;) This will default to your current directory.

    Also if your directory structure is very deep and you're just interested in the immediate depth you can pass the -L parameter to limit how deep tree recurses:
    $ tree -L 2

  2. Nice tool, can be quickly installed using "yum install tree" on centos / fedora etc.

    One point to note, when using a putty shell you can use the -A switch to print ANSI indentation lines. Otherwise you could end up with a bunch of unreadable junk, like so...

    "â   â   â   âââ prev-horizontal.png"

    (also -S prints ASCII)

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>