The Tree Command on Debian

Most Linux users utilize the good old ls command for a directory listing on Debian. The ls command, however, lacks some features that are provided by another command- the tree command. This command prints the folders, subfolders, and files in the form of a tree. You can make the command even more useful by using various options/flags with it. In this article, we will explain how you can master the tree command, with the help of a few examples.

We have run the commands and procedures mentioned in this article on a Debian 10 Buster system.

How to Install the Tree command?

Installing the tree command-line utility on Debian is pretty simple through the apt-get command. Open the Debian command line, the Terminal, through Application Launcher search as follows:

Debian Terminal

The Application Launcher can be accessed through the Super/Windows key on your keyboard.

Then enter the following commands as sudo:

$ sudo apt-get update

(We recommend running this command before each install so that you can get the latest available version of a software present in the online repositories)

And then,

$ sudo apt-get install tree

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

Install tree command

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

$ tree --version

Check tree command version

How to use the 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

Using Tree command on Linux

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

Display contents of a specific directory

In order 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 command view of a directory

Display hidden files along with other files using Tree

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

$ tree -a

Tree -a

The files and folders in the tree starting from a ‘.’ are the hidden ones. In the above output, I have highlighted one such entry to explain how 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

Tree -d

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 list.

$ tree -f

Tree -f

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

To 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

Tree -s

This helps you in determining which items are taking a large amount of space on your system and getting 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

Tree -p

So before you want to perform an operation on a file and folder, you can first know and may be edit, the permissions you have on a specific item.

List folder contents till 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

Tree command levels

Make The Tree command print file listing containing a specific pattern

You can use the tree command to 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 containing the keyword “screenshot”:

$ tree -P *screenshot*

Tree -P

Make the Tree command avoid printing some selective 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

Tree -d -I

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 usage further by viewing the help of the tree command as follows:

$ tree --help

Tree command help

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