Plant a Tree in the Command Line

Happy 50th Anniversary of Earth Day!

Not all of us can plant a tree or do other restorative work for Earth Day, and in the face of large-scale problems to solve we don’t always feel like the small things we do to help (reducing the amount of water or electricity we use) matter. But the small things do help. For me, this recently was highlighted in another part of my life in the form of a handy little command line tool.

I was feeling that having to enter cd (change directory) and ls (list directory contents) commands over and over to examine the contents of the file system on the command line was an inefficient way to get a good picture of the files and directories. This is more frequently an issue when I download software to install or am getting familiar with files for a project already under way. What kinds of files are there, and how are they organized?

I performed some simple web searches and found tree. It outputs a recursive directory listing in an easy-to-read tree format. It also provides a variety of options to filter what will be displayed and whether extra information beyond the file name is shown.

tree command listing all files and directories of a python project
Figure 1

Install tree

tree is available on a large variety of operating systems. Linux, Mac, and even Windows with cygwin. I’ll just share the simplest ones for quick reference.

Fedora / CentOS / Red Hat / Other YUM-based Systems

sudo yum install tree

Debian / Ubuntu / Other APT-based Systems

sudo apt-get install tree

Mac OS with Brew

brew install tree

Example Usage

Running tree with no arguments will recursively list every single file and directory from the current directory. This is usually far too much information to be useful for a quick glance to get the big picture of file system organization. So here are a couple options to help narrow output to something more useful:

Colorize Output

tree -C

This was used in Figure 1. Without this option, tree will display all files and directories with the same color. This option may not be as helpful for individuals with color-blindness or limited sight, but customizing the LS_COLORS environment variable which controls the colorization may help there. I personally use Dark Solarized colors via dircolors-solarized. David Newcomb has a blog post digging further into configuring LS_COLORS.

To always get colorized output without needing to specify the -C option every time, create an alias to apply it for you with alias 'tree'='tree -C'. Now calling tree from the command line will include the colorization option automatically. Creating the alias this way is temporary, but you can make this persist across all future terminals by adding it to your Bash startup script(s).

Limit Recursion Depth

tree -L 3

This limits the depth of directories tree will traverse for listing files and directories. This limits output to more of a surface-level picture of a directory’s contents.

tree command listing top level files and directories of a python project
Figure 2

Only Display Directories

tree -d

This limits tree to only list directories, for a more structural examination of a directory’s contents.

tree command listing only directories of a python project
Figure 3

List Only or Ignore Files Matching a Pattern

tree -P *.png

This option will search the files and only list files which match a pattern (which follows the Bash globbing pattern syntax) and the intermediary parent directories of those files.

tree command listing files of a python project matching a pattern
Figure 4

tree -I *.png

This option will display the inverse by ignoring all files which match the pattern entered.

tree command listing files of a python project not matching a pattern
Figure 5

Ignore Directories with Lots of Files

tree --filelimit 4

This will stop tree from recursing down into directories which contain more than the specified total number of files and directories at the next level down. This is another way to limit things to a more surface-level view and shorten the length of output.

tree command listing files and directories which don't contain too many files of a python project
Figure 6

Show Permissions

tree -p

This shows permissions alongside the files and directories.

tree command listing all files and directories of a python project with permissions info
Figure 7

Display Another Directory

tree -CI *.log -L 2 /var/local/www/data

This command is combining a few of our examples, but the new part is just specifying a different directory at the end. This can be used to help minimize the repeated cd command usage I mentioned earlier.

tree command listing files and directories of another directory
Figure 8

Reduce Usage

Much like tree has helped me reduce my usage of the cd and ls commands, I hope for Earth Day you will take some time to reflect on your own habits and reduce usage of resources, whether it be disposable products as opposed to reusable ones, containers which can’t be recycled, water in the shower, gasoline for driving (take it easy on the accelerator), gas for heating, or electricity for lighting and appliances.

I hope you found this helpful and have a healthy day!