The “tree” Command in Linux [14 Practical Examples]

Sometimes it becomes essential to explore the file structure of a new server, organize files and directories on a personal computer, and debug issues with file permissions or directory structure. Consequently, the ‘tree’ command in Linux simplifies these tasks by providing an easy-to-read, hierarchical view of the file system. In this article, I will explore the features and uses of the ‘tree’ command in Linux, providing multiple examples along the way.

A. Description

The tree command in Linux is a command-line utility that recursively lists the contents of a directory in a tree-like format, with each directory shown as a folder and each file shown as a file. The tree command also shows the file permissions and other attributes of each file and directory. It is useful for visualizing the hierarchical structure of a file system and for quickly navigating through complex directory structures.

B. Syntax

The syntax of the tree command in Linux is pretty simple, as shown below.

tree [OPTION]... [DIRECTORY]...

Note: In the syntax above, the OPTION & DIRECTORY enclosed by the square brackets means it is not mandatory and three dots after the square bracket mean multiple OPTIONS & DIRECTORIES can be used after the tree command.

C. Options

There are numerous options available for the tree command. I have listed a few of them. You can learn more about the tree command, its options and their uses by checking the man page.

man tree

Note: As the tree command is not a part of standard Linux so to see the man page of the tree command you have to install the command first. You can check the installation process in the next section.

Useful Options

  • -a: All  files( including hidden files) are printed.
  • -d: List directories only.
  • -f: Display the full path prefix for each file.
  • -L: Max displays the depth of the directory tree.
  • -P pattern: List only those files whose names match the wild character “pattern”.
  • -I pattern: Don’t list those files whose names match the wild character “pattern”.
  • -o: Send output to any file.
  • -Q: Quote the names of files in double quotes.
  • -p: Display the file type and permissions for each file.
  • -s: Print the file size.
  • -h: Print the size of each file in a human-readable way.
  • -D: Display the last modification date.
  • –device: Prints the device number to which the file or directory belongs.
  • -r: Sort the output in reverse order.
  • -t: Sort the file depending on the last modification time.
  • –filelimit: Used to limit the number of files or directories displayed in the output.
  • –version: Display the version of the tree command.
  • –help: Display the help information of the tree command.

Note: Commands and their options are case-sensitive in Linux. So, be careful while using them.

Installing the “tree” Command in Linux

The tree command is not a part of the standard Linux distribution, so it needs to be installed separately. However, it is widely available and can be easily installed on most Linux distributions. You can find out whether you have it on your machine or not by typing the command tree itself.

tree

Your system will print out the following message if the tree command is not already installed.Displaying confirmation message for the execution of the 'tree' command in Linux.Otherwise, you will get a list of files and directories that indicates the command has already been installed.

If you find that the tree command is not installed, don’t worry. You can install the tree command on your machine within a minute by following the instructions below.

Steps to Follow:

➊  At first open the Ubuntu Terminal

➋  Type any of the following commands in your command prompt:

sudo apt-get install tree

Alternatively,

sudo apt install tree

Alternatively,

sudo snap install tree

➌ Tap the ENTER key.

➍ Input your password

➎ Now, press ENTER

Output:

Upon completion of these steps, the tree command will be installed on your machine.Showing the installation confirmation massage.

Practical Examples of the “tree” Command in Linux

In Linux, the “tree” command is a helpful tool to visualize the hierarchical structure of a file system, which helps you to navigate quickly through complex directory structures. In the section below, I will show you some of the most useful applications for the tree command in Linux.

Example 1: Display Contents of a Directory With the “tree” Command in Linux

The tree command in Linux allows you to display the contents of a specific directory in a tree-like format, which is helpful to visualize the hierarchical structure. Here, I will show you the list of directories and files inside my current directory. You can do the same by following the instructions below.

Steps to Follow:

➊ At first, open the Ubuntu Terminal.

 Type the following command into the command prompt:

tree

➌ Now, press the ENTER button.

Output:

In the following image, you can see the list of contents inside my current directory.Showing the list of contents inside my current directory.

Note: You can use the directory name or directory path after the tree command to see the contents of any specific directory.Showing the use of directory path with tree command.

Example 2: Display Hidden Files Along With Other Files

You can display the hidden files (The name of the hidden files starts with a dot ”.”)along with the regular contents of a directory by using the option -a after the tree command. In this example, I will display the hidden files along with the regular contents of the “manual” directory using the tree command. You can do the same for any directory by following the steps below.

Steps to Follow:

➊ At first, open the Ubuntu Terminal.

 Type the following command into the command prompt:

tree -a

➌ Now, press the ENTER button.

Output:

In the image below, you can see the hidden file named “.abc.txt,” which was invisible in the previous image.Showing the hidden file named ".abc.txt," along with other file.


Similar Readings


Example 3: Display Only the List of Directories Using the “tree” Command in Linux

Using the tree command in Linux followed by the option -d, you can display only a list of directories held by any directory. Here, I will show you only the list of directories contained in the “manual” directory. You can do the same by following the steps below.

Steps to Follow:

➊ At first, open the Ubuntu Terminal.

 Type the following command into the command prompt:

tree -d

➌ Now, press the ENTER button.

Output:

The list of only directories contained in the “manual” directory is shown in the accompanying image.Showing the list of only directories contained in the "manual" directory with the tree command in linux.

Example 4: Display Full Path of Files and Folders as Prefix Using the “tree” Command in Linux

You can modify the output of the tree command by using the option -f after the tree command. Here, I will show you the path of all files and the directory as a prefix to the file name using the tree command followed by the option -f. You can do the same by following the steps below.

Steps to Follow:

➊ Launch the Ubuntu Terminal.

 Type the following command into the command prompt:

tree -f

➌ Now, press the ENTER button.

Output:

The list of files & directories contained in the “manual” directory is shown in the image below with their full paths as prefixes.Showing the list of files & directories contained in the "manual" directory with the tree command in linux.

Example 5: Display the Size of Files and Folders Using the “tree” Command in Linux

You can show the file and directory size in front of that file or directory name by using the -s or -h options. where the option -h will show the result in a more human-readable way. In this example, I will show you the file size prior to the file name using the option -h.  To do the same, you may follow the steps below.

Steps to Follow:

➊ First, open the Ubuntu Terminal.

 Type the following command into the command prompt:

tree -h

➌ Now, press the ENTER button.

Output:

In the following image, you can see that the size of the contents is shown before their name.Showing the size of the contents is shown before their name with the tree command in linux.

Example 6: Display Read-Write Permission of Files and Folders Using the “tree” Command in Linux

The tree command is capable of showing you each file’s permissions list with the help of option -p. Here, I will show you an example of that. You can get the same result by following the steps below.

Steps to Follow:

➊ First, open the Ubuntu Terminal.

 Type the following command into the command prompt:

tree -h

➌ Now, press the ENTER button.

Output:

In the image below, you can see the permission list of each file prior to the file name.Showing the permission list of each file prior to the file name.

Example 7: List Contents to a Certain Level/Depth Using the “tree” Command in Linux

You can set the tree command to display the tree at a specific level or path rather than listing every item in our directory. For instance, level 1 of the tree command can only show the list of the directory and files, not any of its subdirectories or files inside the first-level directories. In this example, I will show you the first-level directory and files inside the directory named “manual.”  You can do the same by following the steps below.

Steps to Follow:

➊ Open the Ubuntu Terminal.

 Type the following command into the command prompt:

tree -a -L 1

➌ Tap the ENTER button.

Output:

The below image shows you the first-level items inside the “manual” directory.Showing the first-level items inside the “manual” directory.

Example 8: Print the Device Number to Which the File or Directory Belongs

The tree command allows you to print the device number to which the file or directory belongs. To print the device number before the file name, you have to use the –device option after the tree command. In this section, I will show you an example of this. To do so, you have to follow the steps below.

Steps to Follow:

➊ Open the Ubuntu Terminal.

 Type the following command into the command prompt:

tree --device

➌ Tap the ENTER button.

Output:

The below image shows you the device number prior to the file or directory name where the items belong.Showing the device number prior to the file or directory name where the items belong.

Example 9: Redirect the Output of the “tree” Command to A File

You can redirect the output of the tree command to a file by using the option -o. In this case, the output won’t be shown at the terminal. In this example, I will redirect the output to a text file named “file.txt”. By using the steps below, you can accomplish the same thing.

Steps to Follow:

➊ Launch the Ubuntu Terminal.

 Execute the following command.

tree -o file.txt

➌ Tap the ENTER button.

➍ Now, run the command to see the redirected output

cat file.txt

➎ Hit ENTER again.

Output:

The image in the section below shows the process of redirecting the output of the tree command and the redirected output through the cat command.Showing the process of redirecting the output of the tree command and the redirected output through the cat command.

Example 10: Print the File Names in Double-Quotes Using the “tree” Command in Linux

The tree command in Linux allows you to show the output in double quotes. For this purpose, you have to use the -Q option after the tree command. In this section, I will show you how to use option -Q with the tree command. To do the same, you can follow the steps below.

Steps to Follow:

➊ Open the Ubuntu Terminal.

 Run the following command.

tree -Q

➌ Press the ENTER button.

Output:

In the image below, you can see that the outputs are shown in double-quotes.Showing the outputs are in double-quotes with tree command in linux.

Example 11: Sort the Files in Reverse Alphabetical Order Using the “tree” Command in Linux

The tree command in Linux allows you to sort the contents of a directory according to the first letter of their name. Now, I will show you the process to sort the directory contents reverse alphabetically using the option -r after the tree command. You can do the same by following the mentioned steps.

Steps to Follow:

➊ Open the Ubuntu Terminal.

 Type the following command into the command prompt:

tree -r

➌ Hit the ENTER button.

Output:

The below image shows the contents of the “manual” directory sorted reverse alphabetically.Showing that the contents of the “manual” directory sorted reverse alphabetically.


Similar Readings


Example 12: Sort Files Based on the Last Modification

You can sort files according to the last modification time with the tree command in Linux. In this example, I will sort the files according to the last modification time using option -t. Here, I will also use option -D to display the last modification date as well. You will be able to do the same by following the steps below.

Steps to Follow:

➊ Launch the Ubuntu Terminal.

 Type the following command into the command prompt:

tree -tD

➌ Tap the ENTER button.

Output:

In the following image, you can see that I have sorted the files according to the last modification time.Showing that the files are shorted according to the last modification time.

Example 13: Get Help About the “tree” Command in Linux

In Linux, the help page for the tree command contains a brief description of the command. You can see the help page by following the steps that I mentioned below.

Steps to Follow:

➊ At first, open the Ubuntu Terminal.

 Type the following command into the command prompt:

tree --help

➌ Now, press the ENTER button.

Output:

In the image below, you can see the help page for the tree command in Linux.Showing the help page for the tree command in Linux.

Example 14: Get the Version of the “tree” Command in Linux

You can see the current version of the tree command on your Linux machine with the –version option. Here, I will show you the version of the tree command for my machine on the terminal. You can do the same by following the steps below:

Steps to Follow:

➊ Open the Ubuntu Terminal.

 Run the following command:

tree --version

➌ Press the ENTER key.

Output:

In the following image, you can see that I have displayed the version of the tree command in Linux.Showing the version of the tree command in Linux.

Conclusion

In this article, I’ve discussed the tree command, which is useful for exploring the file structure, organizing files and directories on a personal computer, and debugging issues with file permissions or directory structure. At the same time, I’ve discussed its options and provided some relevant examples. Hopefully, this article will help you better understand the tree command and its use. Consequently, it will make your journey easier to become a power user of the Linux command line.


Similar Readings

Rate this post
S. M. Amdadul Islam

S. M. Amdadul Islam

Hello everyone. I am S. M. Amdadul Islam, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Naval Architecture and Marine Engineering (NAME) graduate from the Bangladesh University of Engineering and Technology. In addition to my regular work, I enjoy watching Youtube, discovering new things, gossiping with friends, visiting new places, and taking photos of landscapes. Read Full Bio

We will be happy to hear your thoughts

Leave a reply

LinuxSimply
Logo