The command-line interface of Linux-based distros works as a catalyst to the actions performed by the users. Like other distros, Ubuntu also supports a variety of commands, and among those, directory handling commands are used to handle the operations related to directories, such as the “cd” command is used to change the current working directory; “mkdir” command is used to make a new directory. Similarly, tree command prints directories, sub-directories, and their content in a tree-like structure. It is almost like an “ls” command, but the main difference is the print pattern of both commands: the “ls” command lists down the content, whereas the “tree” command shows the output in a tree-like hierarchical structure. In this demonstration, we will provide a deep insight into the usage of tree commands with examples. So, let’s start with the installation of the tree:

How to install Tree command in Ubuntu

Firstly, you have to install the tree as it is not available by default; so, if you have not installed the tree command yet, then use the following command to install it:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/1-40.jpg" data-lazy- height="456" src="data:image/svg xml,” width=”1365″>

How to use Tree command in Ubuntu

Once it is installed; execute the following command to get the detailed usage of the tree command. The syntax of tree command is:

tree [options] [directory]

The syntax consists of two items: options and directory: options are used to display the result of tree command according to some specific property. For instance, options can be used to get the output in various ways, such as: in alphabetical order, to display only directories, to check the size and location of files or directories. However, If you want to get the result of a specific directory, then you will put the directory name in replace of the “directory” keyword that is used in syntax.

General command output : You can get the print (on the terminal) of the directory’s content in the tree structure by executing the following command. The command will print the content of the present working directory:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/2-38.jpg" data-lazy- height="552" src="data:image/svg xml,” width=”797″>

Tree command has displayed a total of 18 directories and 13 files:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/3-37.jpg" data-lazy- height="74" src="data:image/svg xml,” width=”689″>

How to use options with tree command in Ubuntu

We have seen the general use of tree command; moreover, there are various options that are used to perform specific tasks within the present directory or the path of any other directory.

How to use -a option with tree command

Generally, the tree command does not print the content of hidden directories; this option helps to print the content of hidden directories as well; the syntax to use this flag is shown below:

tree -a [directory]

The command given below will help to get the hidden content of the present working directory:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/4-34.jpg" data-lazy- height="549" src="data:image/svg xml,” width=”797″>

You can observe that the tree command only printed 18 directories and 13 files, whereas; the same command, when used with the “-a” flag, displayed 215 directories and 256 files.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/5-32.jpg" data-lazy- height="69" src="data:image/svg xml,” width=”738″>

How to use -d option with tree command

This option is used with tree command to only list down the directories of the targeted directory; the syntax of tree command with the option “-d” is given below:

tree -d [directory]

The command given below will only print the directories inside the current directory:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/6-29.jpg" data-lazy- height="543" src="data:image/svg xml,” width=”795″>

Other than the present working directory; For, instance you want to get the directories list of the “Desktop” directory, then execute the following command to do so:

$ tree -d /home/adnan/Desktop

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/7-28.jpg" data-lazy- height="216" src="data:image/svg xml,” width=”734″>

How to use -f option with tree command

Tree command, when used with the “-f” option, shows the path prefix of every file in a tree structure: syntax to use this option is shown below:

tree -f [directory]

The command given below will show the path of every directory or file present in the current directory:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/8-24.jpg" data-lazy- height="543" src="data:image/svg xml,” width=”793″>

Moreover, you can get the result of this command for any directory other than pwd; For instance, to apply the “-f” flag on the “Desktop” directory; use the path of the directory, or you can pass the name of the directory if it is sub-directory of your pwd.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/9-22.jpg" data-lazy- height="219" src="data:image/svg xml,” width=”736″>

How to use -o option with tree command

With the help of using this option, you can get the output of the tree command in any other file; the syntax to use this flag is mentioned below:

tree [directory] -o [filename]

The below-stated command will save the output of the tree command to a file named “output.txt“:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/10-21.jpg" data-lazy- height="547" src="data:image/svg xml,” width=”798″>

You can get the output of any specific directory for this option: the command below will save the output of the tree command (Desktop directory) in the “output1.txt” file:

$ tree Desktop -o output1.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/11-17.jpg" data-lazy- height="236" src="data:image/svg xml,” width=”737″>

How to use -Q option with tree command

This file listing option of a tree will display the names of files in double quotations; syntax to use this flag is given below.

tree -Q [directory]

To print the file names in double-quotes; execute the following command to get the output of the present working directory:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/12-14.jpg" data-lazy- height="542" src="data:image/svg xml,” width=”807″>

Moreover, the command given below will show the file and directories name (of Desktop directory) in double-quotes:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/13-12.jpg" data-lazy- height="224" src="data:image/svg xml,” width=”738″>

How to use -p option with tree command

By using the “-p” flag, all the file types and permissions to each file are printed in a hierarchical structure: the syntax of the tree along with this option is given below:

Syntax

tree -p [directory]

the command given below will show the use of this option on the present working directory:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/14-11.jpg" data-lazy- height="490" src="data:image/svg xml,” width=”806″>

Using the “-p” flag on the directory named as “directory1“; that is present in our home directory: the following command will give the result of the “-p” flag on “directory1“:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/15-14.jpg" data-lazy- height="270" src="data:image/svg xml,” width=”738″>

How to use -s option with tree command

You can get the display of size (in bytes) of all files by using the “-s” flag; the syntax is given below:

tree -s [directory]

The command given below will show the size of the files inside the current working directory:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/16-10.jpg" data-lazy- height="332" src="data:image/svg xml,” width=”807″>

To check the size of a specific directory; pass the path, or you can pass the name only if the directory lies in the present working directory: the following command will print the size of files present inside “directory1“:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/17-6.jpg" data-lazy- height="272" src="data:image/svg xml,” width=”736″>

How to use -h option with tree command

This option will also display the size of each file and directory, but in KB’s, MB’s, GB’s, etc. The “-s” flag displays the result in bytes, but the result contains only numeric values; a novice user may not get the measuring unit.

tree -h [directory]

The command given below will help to get directories, files, and their sizes in tree shape:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/18-6.jpg" data-lazy- height="468" src="data:image/svg xml,” width=”806″>

The command mentioned below will get to the result of “-h” for “directory1“:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/19-5.jpg" data-lazy- height="273" src="data:image/svg xml,” width=”737″>

How to use -t option with tree command

This option will sort the files and display the output as the modification time of the files; the syntax of the “-t” flag with tree command is given below:

tree -t [directory]

The command given below will print the files according to their modification time in alphabetical order:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/20-4.jpg" data-lazy- height="546" src="data:image/svg xml,” width=”804″>

In our case, the following command will display the result of the “-t” flag (on the “Desktop” directory):

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/21-2.jpg" data-lazy- height="227" src="data:image/svg xml,” width=”736″>

How to use -r option with tree command

This option will sort the output of the tree command in reverse alphabetical order; the syntax is shown below:

tree -r [directory]

The command given below will print the files in reverse alphabetical order of the current directory:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/22-1.jpg" data-lazy- height="493" src="data:image/svg xml,” width=”818″>

To get the alphabetic order list of files present in “directory1“, execute the following command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/23-1.jpg" data-lazy- height="279" src="data:image/svg xml,” width=”740″>

How to use -L option with tree command

This option will limit the number of branches of tree command; the syntax to use this option is given below:

tree -L [numeric number] [directory]

This option will limit the number of branches of a tree to 1:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/24-2.jpg" data-lazy- height="518" src="data:image/svg xml,” width=”806″>

To get the shortlist of the “directory1” directory; the following commands will do the job:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/25-1.jpg" data-lazy- height="237" src="data:image/svg xml,” width=”740″>

Conclusion

Ubuntu supports several useful commands to handle directories and files; tree command is one of them. In this guide, the tree command is explained in detail; its basic use is to print the directories and files in a tree-like hierarchical structure; moreover, tens of flags are supported by a tree command that shows the output with specific properties. One can join multiple options at a time to attain more purified results in a single command.