Introduction to tree

If you spend any amount of time on the command line, chances are you sometimes want some kind of visual representation of directory structure. Fortunately there is a small and easy-to-use command line application that can help with this task. Known as tree, this is a command available for most all Linux, Unix and Unix-like systems. This blog post will give an overview of tree, how to install it, some of its options as well as some uses applicable to both local and server situations.

Installing tree

Depending on your operating system, you may already have tree installed. If you receive command not found when entering tree on your command line, though, you will need to install it before using it.

For Mac OS X, the easiest way to install tree is via Homebrew, by running the following command:

brew install tree

 

tree can be installed on most Linux systems by using the following command:

apt-get install tree

 

You may need to prefix sudo before either of the above commands, depending on user permissions of the account you’re logged into.

Using tree

Once you have tree installed, the easiest way to start using it is by simply entering tree on the command line. This will then print or cat a recursive visual representation of the file structure you’re currently in. Your output should look something like this:

Introduction to tree

If you’d like to tree a different directory then the one you’re currently in, you can simply add the file path for the directory, such as tree ~/path/to/directory.

There are a wide assortment of options and flags that can be added to the basic tree command to modify its output. We’ll go over some of the most common and useful ones.

By default, tree will recursively output all sub-folders and files within the default or specified directory. For a directory containining numerous levels of sub-folders and files, this may be more detail than you need. You can specify the “depth” of the output by using the -L or level flag after tree, followed by a number to indicate the levels, such as tree -L 2.

The same directory as above would display as like below when running tree -L 1:

Introduction to tree

tree -C

The -C flag will add color highlighting to files and folders at different levels, better improving the readability of the file structure:

Introduction to tree file structure

By default tree does not display hidden files or (.) dot files. You can output all files by adding the -a flag to the command: tree -a

If you only want to view directories, with no files represented, you can use the -d command: tree -d.

-f adds the full path prefix for each file listed:

Introduction to tree command

While printing out the file structure of a directory is useful, chances are sometimes you’ll want to save this output to a file for later reference. The easiest way to do this is via the > and >> re-directions commands.

Both > and >> will “re-direct” tree‘s output somewhere else, such as a text file we provide the name for, even if it doesn’t yet exist:

tree >> new_file.txt

The output from tree will now be saved in new_file.txt.

The one difference between > and >> is that the former one appends to a file (existing or new), while >> will overwrite all previous existing content of the file.

Resources

Post a Comment

Comments are moderated. Your email is kept private. Required fields are marked *

© 2019 Sunlight Media LLC