How to use the tree command in Linux?

Edit File In Linux

The tree command recursively lists directories and files in Linux. Files are listed in an indented manner under their directories.

Tree command is similar to ls command which is famously known for listing files and directories. The only difference is that tree command outputs the files and directories in a tree-like manner. This command is not installed by default.

Using the tree command in Linux

In this tutorial we will learn how to install and use tree command in Linux.

1. Installing the tree command

To install tree command in your Linux system use the apt command:

sudo apt install tree

This will successfully install tree command in your system.

To check the version of tree command use the following command:

tree --version
Tree Version
Tree Version

2. Running tree command without any arguments

In any directory with multiple files/directories, enter the tree command without any arguments as shown below:

tree
Tree Output

The output displays the files and directories in your current working directory. Notice how contents of a directory are indented. At the very bottom you will find the total number of directories and files.

Total Number Of Files And Directories

3. List contents of a particular directory

To list the contents of a particular directory, mention the name along with the tree command.

tree test_dir
Test Directory

You can also mention the path of the directory if it is inside another directory.

tree path/dir_name

4. Display hidden files in tree output

To show the hidden files use -a flag along with tree command.

tree -a 
Hidden File

You can see files beginning with a dot in the list.

5. List only directories

To only list directories in a tree like fashion use the -d flag along with tree command.

tree -d
Tree Command

The output only shows directories.Total count of directories is given at the bottom.

6. List with complete path prefix

To list the files along with complete path prefix use -f flag with tree command.

tree -f
Tree With Complete Path

7. Sorting the tree command output

Tree command also gives you options to sort the output. These options are as follows:

  • -v: Sort the output by version.
  • -r:Sort the output in reverse alphabetic order.
  • -t: Sort the output by last modification time instead of alphabetically.

The syntax is as follows :

tree [option] 
tree -t
Sort T
tree -r
Sort -r

8. Display additional information in output

You can use flags to display additional information in the list. Information such as file permission, user ID. The flags for displaying additional information are as follows:

  • -p: Print the file type and permissions for each file (same as ls -l).
  • -u: Print the username, or UID (if no username is available) of the file.
  • -s: Print the size of each file in bytes along with the name.
  • -D: Print the date of the last modification time for the file listed.

The syntax is as follows:

tree [option]
tree -p
Tree P
tree -u
Tree U

Conclusion

This tutorial was about tree command in Linux. Tree command is useful for understanding the file structure your system. For more information on tree command refer to its man page.