Linux ‘tree Command’ Usage Examples for Beginners

The tree is a tiny, cross-platform command-line program used to recursively list or display the content of a directory in a tree-like format. It outputs the directory paths and files in each sub-directory and a summary of a total number of sub-directories and files.

Read Also: dutree – A CLI Tool to Analyze Disk Usage in Coloured Output

The tree program is available in Unix and Unix-like systems such as Linux, as well as DOS, Windows, and many other operating systems. It features various options for output manipulation, from file options, sorting options, to graphics options, and support for output in XML, JSON and HTML formats.

In this short article, we will show how to use the tree command with examples to recursively list the contents of a directory on a Linux system.

Learn tree Command Usage Examples

The tree command is available on all if not most Linux distributions, however, if you do not have it installed by default, use your default package manager to install it as shown.

# yum install tree	 #RHEL/CentOS 7
# dnf install tree	 #Fedora 22+ and /RHEL/CentOS 8
$ sudo apt install tree	 #Ubuntu/Debian
# sudo zypper in tree 	 #openSUSE

Once installed, you can proceed further to learn the tree command usage with examples as shown below.

1. To list directory content in a tree-like format, navigate to the directory you want and run tree command without any options or arguments as follows. Remember to invoke sudo to run the tree in a directory that requires root user access permissions.

# tree
OR
$ sudo tree

It will display the contents of the working directory recursively showing sub-directories and files, and a summary of the total number of sub-directories and files. You can enable the printing of hidden files using the -a flag.

$ sudo tree -a
List Directory Content in Tree Format
List Directory Content in Tree Format

2. To list the directory contents with the full path prefix for each sub-directory and file, use the -f as shown.

$ sudo tree -f
List Directory Content with Location
List Directory Content with Location

3. You can also instruct tree to only print the subdirectories minus the files in them using the -d option. If used together with the -f option, the tree will print the full directory path as shown.

$ sudo tree -d 
OR
$ sudo tree -df
List Sub-Directory Paths
List Sub-Directory Paths

4. You can specify the maximum display depth of the directory tree using the -L option. For example, if you want a depth of 2, run the following command.

$ sudo tree -f -L 2
Set Directory Listing Depth
Set Directory Listing Depth

Here is another example about setting maximum display depth of the directory tree to 3.

$ sudo tree -f -L 3
Set Maximum Directory Listing Depth
Set Maximum Directory Listing Depth

5. To display only those files that match the wild-card pattern, use the -P flag and specify your pattern. In this example, the command will only list files that match cata*, so files such as Catalina.sh, catalina.bat, etc. will be listed.

$ sudo tree -f -P cata*
List Files by Wild Card Pattern
List Files by Wild Card Pattern

6. You can also tell the tree to prune empty directories from the output by adding the --prune option, as shown.

$ sudo tree -f --prune

7. There are also some useful file options supported by tree such as -p which prints the file type and permissions for each file in a similar way as the ls -l command.

$ sudo tree -f -p 
Print File Type and Permissions
Print File Type and Permissions

8. Besides, to print the username (or UID if no username is available), of each file, use the -u option, and the -g option prints the group name (or GID if no group name is available). You can combine the -p, -u and -g options to do a long listing similar to ls -l command.

$ sudo tree -f -pug
Print File Type and Permissions with Owner
Print File Type and Permissions with Owner

9. You can also print the size of each file in bytes along with the name using the -s option. To print the size of each file but in a more human-readable format, use the -h flag and specify a size letter for kilobytes (K), megabytes (M), gigabytes (G), terabytes (T), etc..

$ sudo tree -f -s
OR
$ sudo tree -f -h
Print File Size
Print File Size

10. To display the date of the last modification time for each sub-directory or file, use the -D options as follows.

$ sudo tree -f -pug -h -D
Show Date of Last Modification Time
Show Date of Last Modification Time

11. Another useful option is --du, which reports the size of each sub-directory as the accumulation of sizes of all its files and subdirectories (and their files, and so on).

$ sudo tree -f --du

12. Last but not least, you can send or redirect the tree’s output to filename for later analysis using the -o option.

$ sudo tree -o direc_tree.txt

That’s all with the tree command, run man tree to know more usage and options. If you have any questions or thoughts to share, use the feedback form below to reach us.

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

6 Comments

Leave a Reply
  1. Hi,

    Thanks for the article.

    Examples for “tree -f -L X” are mixed. The output of “tree -f -L 3” is the one of “tree -f -L 2“.

    Regards,

    Reply

Leave a Reply to Aaron Kili Cancel reply

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.