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:
2. Running tree command without any arguments
In any directory with multiple files/directories, enter the tree command without any arguments as shown below:
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.
3. List contents of a particular directory
To list the contents of a particular directory, mention the name along with the tree command.
You can also mention the path of the directory if it is inside another directory.
4. Display hidden files in tree output
To show the hidden files use -a flag along with tree command.
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.
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.
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 :
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:
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.