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
$ 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 
$ 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.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
$ 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.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

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.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide
The Complete Linux System Administrator Bundle
Become an Ethical Hacker Bonus Bundle

You may also like...

5 Responses

  1. Daniel Kubong says:

    Aaron, the tree command will not run on Centos 7, can you confirm what distro/version of Linux you’re using?

  2. Gustavo Fernández says:

    In openSUSE command for install is:

    $ sudo zypper in tree

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.