How to Master the Linux Tree Command

As a Linux user, our first choice for directory listings is the good old ls command. The ls command, however, lacks some functions that the topic discussed here offers - the tree command. This command outputs the directories, subdirectories, and files as a tree. You can make the command even more helpful by giving it different options/flags to customize the list.

This article explains how to use the tree command with examples.

We have run the commands and procedures mentioned in this article on an Ubuntu 22.04 LTS system.

How to Install the Tree Command

Installing the tree command line utility is pretty simple through the apt-get command. Open your Ubuntu command line, the Terminal, either through the system Dash or the Ctrl+Alt+t shortcut.

Then enter the following command as sudo:

$ sudo apt-get install tree

Please note that only an authorized user can add, remove and configure software on Ubuntu.

Install the tree command

After the tree command is installed, you can check the version number and also ensure that the installation was successful through the following command:

$ tree --version

Check tree command version

We recommend running the following command before each install so that you can get the latest available version of software present in the online repositories:

$ sudo apt-get update

How to Use Tree command

Here we will mention some examples of the tree command so that you can not only use it but also take a step forward in mastering it.

Basic Tree output

This is the most basic way of using the tree command:

$ tree

Output of tree command

The output shows a tree structure of your current directory, displaying all the folders, sub-folders, and files.

Display contents of a specific directory

To list the files and subfolders of a specific directory rather than that of the current directory, you can specify the directory name or path through the following command syntax:

$ tree -a [DirectoryName/Path]

Example:

The following command will list all the files and sub-folders, if any, in the Pictures directory:

$ tree -a Pictures

Tree output of specific directory

Display hidden files along with other files using Tree

The tree command does not display the listing of hidden files and folders in Ubuntu. You can, however, use the ‘a’ flag as follows in order to list them:

$ tree -a

Display hidden files with tree

The files and folders in the tree starting from a ‘.’ are hidden. In the above output, I have highlighted one such entry to explain what it looks like.

Display only directory listing through Tree

If you want to view only the directory listing and not the underlying files, you can use the d flag with the tree command as follows:

$ tree -d

Show directories only

Display full path prefix of files and folders using Tree

With the -f flag, you can customize the tree flag to display the complete path as a prefix for all the files and folders.

$ tree -f

Display full path prefix of files

This is especially helpful when you want to know what exists where.

Display size of files and folders using Tree

With the -s flag, you can make the tree command print the bytes size of all the files and folders in your directory.

$ tree -s

Display size of files and folders

This helps you determine which items are taking up a large amount of space on your system and get rid of the unnecessary ones.

Display read-write permissions of files and folders using Tree

Through the p flag in your tree command, you can view the read, write and delete permissions on the listed files and folders.

$ tree -p

Display read-write permissions of files and folders

So before you want to operate on a file and folder, you can first know and maybe edit the permissions you have on a specific item.

List folder contents to a certain level/depth through Tree

Instead of listing all the contents of your directory, you can configure the tree command to display the tree to a certain level or depth. For example, level 1 in the tree command will only show the list of the given folder rather than any of its subfolders. Here is how to use the syntax:

$ tree -L [n]

Example:

The following command will display only the sub-directories (with the help of -d flag) of the current directory and not the further expanded tree.

$ tree -d -L 1

List folder contents till a certain level

Make The Tree command print file listing containing a specific pattern

The tree command can only list the files containing a specific wild card pattern. Here is the syntax to specify the pattern:

$ tree -P [[pattern]*]/[*[pattern]]/[[*pattern*]]

Example:

In this example, I am using the tree command to list those files starting with the keyword “touch”:

$ tree -P touch*

Display files that match a certain name pattern

Make the Tree command avoid printing some selective file names

You can also use the tree command to list everything but the files containing a specific wild card pattern.

Syntax:

$ tree -I *[keywords]

Example:

The following command will list all the files and folders except for the one containing the “snap” keyword.

$ tree -d -I *snap

avoid printing some selective file names

Print Tree command output to a file

If you want to print the result of the tree command to a file, you can use the following syntax:

$ tree -o [filename]

Example:

The following command will print the list of all files and folders of the Pictures folder to an HTML file named myfile.html

$ tree ./Pictures -o myfile.html

Tree Help

The tree command is much more helpful than the usage we have described. You can explore the command further by viewing the help of the tree command as follows:

$ tree --help

Tree command help

By using the flags, we described and combinations of these flags, you can master the tree command even more!