How to Use the Linux tree Command – 11 Examples

linux-tree-command

The Linux tree command lets users view the hierarchical structure of a directory. The command recursively lists the contents of the specified directory, showing all its contents at all levels. It also shows the total number of files and sub-directories that are contained.

Our focus for this post will be to understand more about what the Linux tree command does and then give examples of using it with different options for better results. Read on!

The syntax for the Linux tree Command

Its syntax is as follows.

The tree command accepts different options, allowing users to customize the results they wish to get. More details on the options are detailed in the example section.

How to Install the ‘tree’ Package in Linux

The tree command doesn’t come preinstalled. We must install it before we start using it. The command to follow will depend on the Linux distro you use.

For Ubuntu and Debian:

install-tree-command-ubuntu

For RHEL:

For Arch-based distros:

Once you run the install command, check the tree version to verify the package installed.

check-tree-command-version

How to Use the Linux Command – Examples

To use the tree command, add the option you want to use when listing a directory’s contents, then specify the target directory or its path.

1. Show Contents of the Current Working Directory

The primary way of using the tree command in Linux is by running with no option. For this example, we list the hierarchical structure of our current working directory with the below command.

The output shows all the contained directories, and for each, it shows the subdirectories and files.

tree-command-working-directory

2. Include Hidden Files

By default, running the Linux tree command won’t show the hidden files in the tree output. To include the hidden files, add the -a flag.

All hidden files will have a preceding period(.) in their name.

tree-command-show-hidden-files

3. Linux ‘tree’ Command List Output Based on Levels

Depending on your target directory, you can have an instance containing hundreds of subdirectories on different levels. Such an output will overwhelm the standard output and may be inconvenient.

However, you can specify to only show files and subdirectories by specifying the levels. Use the -L option and add the level number. We’ve used level 2 for our example to list the contents of our directory

tree-command-specify-output-level

4. Show Only Directories

The tree command will display files and subdirectories. However, you can omit the files and only display the sub-directories in each level by adding the -d option.

tree-command-show-only-directory

5. Specify Path to Target Directory

So far, we’ve only worked with the current directory to display the tree structure. You can specify the directory’s path in which you wish to display its contents.

Taking /Desktop as our example, we would execute the command below.

tree-command-include-path-to-target

6. Using the ‘tree’ Command with ‘pwd’

Sometimes, you may want to add the path when running the tree command. Instead of manually typing it, you can use the pwd to fetch the current working directory and pass it as an argument for the tree command.

First, let’s change the directory using the cd command. Running the pwd command shows we are in the /usr/bin.

change-directory-with-cd-command

Suppose we wanted to pass the value of the pwd command to our tree command to display the full path of all the contained files and sub-directories. Our command would be:

tree-command-set-path-with-pwd

7. Linux ‘tree’ Command Display Size of a Directory and Files

Even when displaying the hierarchical contents of a directory, you can add the –du option to include the sizes in the result. We’ve also included the -h to give the results in a human-readable format.

In the output, each directory will have its size displayed, and the contained files will also have their sizes shown on the left of their names.

tree-command-display-directory-size

Suppose you only want to see the contained sub-directories and their size. Use -df, and only sub-directories will be displayed, and their sizes will be shown on the left of their name.

display-directory-only-and-size-with-tree-command

8. Sort By Modification Date

When displaying the contents of a directory, you can specify to sort them based on their modification date.

Use the -cD option to sort them based on the first modification.

tree-command-sort-with-modification-date

To reverse the sort and arrange them based on the last modification, use the -cDr option.

tree-command-sort-last-modified

9. Show Full Path in the Tree Structure Output

Add the -f option to display the files’ full path in the output. This way, you will get the full path of all the files and sub-directories.

tree-command-show-full-path

10. Specify Pattern for Displayed Files

When running the tree command, the -P flag lets you specify a pattern. For instance, to display the full path of sub-directories while only showing files that start with ‘lis,’ we would have our command as follows.

Note that the sub-directories won’t be affected, but their files will only be displayed if they match the pattern.

tree-command-specify-pattern

11. Show File Type and Permission

If you want your Linux tree command output to show the file type and permissions of the contained files and sub-directories, add the -p flag.

The command below shows the full path, file type, and permissions.

linux-tree-command-show-file-type-and-permission

You can check the man page for the tree command to get more information on how to use the command.

Conclusion

The Linux tree command displays a tree structure of all the files and sub-directories in the target directory. You can use it with different options to get specific results, and this post has given several examples of using the tree command in Linux. Hopefully, you now understand the tree command and how to utilize it.

Found this helpful? - Share it!

Leave a Comment

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

Scroll to Top