Mastering the tree command in Debian

Most Linux users use the good old ls command to list directories in Debian. However, the ls command lacks some of the functionality provided by another command, the tree command. This command prints folders, subfolders and files in a tree view. You can make the command even more useful by using various options / flags with it. In this article, we will explain how to master the tree command with a few examples.

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

How do I install the tree command?

Installing the tree command line utility in Debian is quite simple with the apt-get command. Open Debian Command Prompt, Terminal, by searching for Application Launcher as follows:

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

Then enter the following commands as sudo:

$ sudo apt-get update

(We recommend that you run this command before every installation so you can get the latest available software version from the online repositories)

And then,

$ sudo apt-get install tree

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

Set tree command

After installing the tree, you can check the version number and also verify that the installation was successful with the following command:

$ tree --version

Check tree command version

How do I use the tree command?

Here are some examples of the tree command so that you can not only use it, but also take you a step further in mastering it.

Basic tree output

This is the simplest way to use the tree command:

$ tree

Using the Tree command in Linux

The output shows a tree structure of your current directory showing all folders, subfolders, and files.

Display the contents of a specific directory

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

$ tree -a [DirectoryName/Path]

Example:

The following command will list all files and subfolders, if any, in the image directory:

$ tree -a Pictures

Directory command tree

Displaying hidden files along with other files using a tree

Tree command does not display the list of hidden files and folders in Debian. However, you can use the “a” flag to list them:

$ tree -a

Tree -a

Files and folders in the tree that start with a “.” Character 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 only want to view the directory listing and not the base files, you can use the d flag with the tree command as follows:

$ tree -d

Tree -d

Displaying the prefix of the full path to files and folders using a tree

With f fag you can configure the tree flag to display the full path as a prefix for all files and folder lists.

$ tree -f

Tree -f

This is especially useful if you want to know what exists and where.

Displaying the size of files and folders using a tree

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

$ tree -s

Trees

This will help you determine which items are taking up a lot of space on your system and get rid of unnecessary ones.

Display read and write permissions for files and folders using a tree

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

$ tree -p

Tree -p

Therefore, before you want to perform an operation on a file or folder, you can first find out and possibly edit the permissions you have for a particular item.

List the contents of a folder to a specific level / depth using a tree

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

$ tree -L [n]

Example:

The following command will only display the subdirectories (using the -d flag) of the current directory, not a further expanded tree.

$ tree -d -L 1

Command levels in the tree

Make the command “Tree” print a list of files containing a specific template

You can only use the tree command to list files that contain a specific wildcard. Here is the syntax for specifying a template:

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

Example:

In this example, I use the tree command to list files containing the “screenshot” keyword:

$ tree -P *screenshot*

Wood -P

Prevent Tree command from printing some selective names

You can also use the tree command to list everything except files that contain a specific wildcard.

Syntax:

$ tree -I *[keywords]

Example:

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

$ tree -d -I *snap

Tree -d -I

Print the output of the Tree command to a file

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

$ tree -o [filename]

Example:

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

$ tree ./Pictures -o myfile.html

Tree reference

The tree command is much more useful than the usage we described. You can further explore the usage by looking at the tree command help as follows:

$ tree --help

Tree Command Reference

By using the flags we have described, as well as using combinations of these flags, you can master the tree command even more.

Mastering the tree command in Debian

Sidebar