Linux tree Command Tutorial for Beginners (6 Examples)

How to Install Snipe-IT Asset Management Software on Debian 9
What’s New in iOS 12? 9 Changes and Features Coming in Fall 2018
Linux namei Command Tutorial for Beginners (5 Examples)

It won’t be wrong to say the ls command is the gold standard when it comes to listing directory contents on the Linux command line. However, there do exist some alternatives that have their own strong points when compared to ls. Once such tool is tree, basics of which we’ll be discussing here in this tutorial.

But before we do that, it’s worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.

Linux tree command

As the name suggests, the tree command in Linux lists contents of directories in a tree-like format. Following is its syntax:

tree [OPTIONS] [directory]

And here’s how the tool’s man page explains it:

Tree is a recursive directory listing program that produces a depth indented listing of files, 
which is colorized ala dircolors if the LS_COLORS environment variable is set and output is to tty.
With no arguments, tree lists the files in the current  directory. When directory arguments are
given, tree lists all the files and/or directories found in the given directories each in turn.
Upon completion of listing all files/directories found, tree returns the total number of files
and/or directories listed.

In case the tree command isn’t installed in your Ubuntu box, you can grab it using the following command:

sudo apt-get install tree

Following are some Q&A styled examples that should give you a good example of how the tree command works.

Q1. How does tree command work?

Basic usage is fairly easy. Just execute the ‘tree’ command sans any option.  Here’s an example:


Here’s the output this command produced on my system:

How does tree command work

So you can see a tree-like structure was produced in the output. Note that it’s the current working directory whose contents are displayed by default. If you want to display contents of any other directory, then you’ll have to specify the path to that directory in input.

Q2. How to make tree display hidden files as well?

By default, the tree command doesn’t display hidden files. However, if you want, you can make tree do that by using the -a command line option.

tree -a

Here’s what the man page says about -a:

All files are printed. By default tree does not print hidden files (those beginning with a dot `.')
. In no event does tree print the file system constructs `.' (current directory) and `..'
(previous directory).

Q3. How to make tree only display directories in output?

Suppose you only want the tree command to display directory entries in output. You can do that using the -d command line option.

For example, the following command:

tree -d

produced this output:

How to make tree only display directories in output

Q4. How to make tree print complete path prefix for files?

This can be done using the -f command line option. Here’s an example:

tree -f

Here’s part of the output produced by this command on my system:

How to make tree print complete path prefix for files

Q5. How to control the depth of directory tree in output?

The tree command also lets you control the depth of directory tree produced in output. This you can do using the -L command line option which requires a numeric value that signifies the permitted directory depth. For example:

tree -d -L 1

The above command would make sure that only first level of directories get displayed in output.

Q6. How to selectively leave some names in output?

The tree command offers a -I command line option that requires you to pass a pattern as input. Depending on the names of files/directories you want not to be displayed in output, create a pattern. For example, I wanted to display only directories, but didn’t want any name with ‘Ubuntu’ pattern (case sensitive), so I used the -I command line option in the following way:

tree -d -I *Ubuntu


While we’ve discussed quite a few tree command line options, the fact is this tool offers a lot more features. Once you’re done practicing what all we’ve discussed here, head to the tree command man page to learn more about it.