Visualizing Directory Structures with the Tree Command

Sometimes good old ls just doesn’t cut it when you’re browsing directories from the command line. Sometimes you just need a more visual overview of the nested files and directories. Well, it turns out there’s a useful command for those cases, and it’s available for Linux, OS X, FreeBSD and just about any OS you can think of. (Except Windows, of course.) It’s called tree.

To install it on OS X, with Homebrew:

brew install tree

On Ubuntu/Debian, or other Linux distros that use Debian packages:

apt-get install tree

To use it, just navigate to a directory and type tree. It has various options (visible with tree --help), so you can limit how many levels down it will go, exclude items that match a pattern, adjust sorting, etc.. You get a spiffy diagram that looks something like this:

├── application.rb
├── boot.rb
├── database.yml
├── environment.rb
├── environments
│   ├── development.rb
│   ├── production.rb
│   └── test.rb
├── initializers
│   ├── backtrace_silencers.rb
│   ├── filter_parameter_logging.rb
│   ├── inflections.rb
│   ├── mime_types.rb
│   ├── secret_token.rb
│   ├── session_store.rb
│   └── wrap_parameters.rb
├── locales
│   └── en.yml
└── routes.rb

Is that cool or what? This should be quite useful for writing tutorials, since a plain-text directory tree is more accessible than a screenshot of a file manager window.