Home Linux Commands How to Limit Depth of ‘tree’ for Recursive File Listing

How to Limit Depth of ‘tree’ for Recursive File Listing

Usually, the ls command is used in Linux to display files and folders. It is an inbuilt command in GNU/Linux. However, it has its shortcomings, for example, there is no option to view directories recursively.

Today we want to introduce you to a new command called ‘tree’ which is used for recursive file listing, and how we can call it with a limit on the depth of file structure to be displayed.

Install Tree Command in Linux

The tree command is not available by default in Linux distributions, and can be installed in Debian and RedHat-based distributions with:

$ sudo apt install tree   [on Debian, Ubuntu & Mint]
$ sudo yum install tree   [on RedHat, CentOS & Fedora]

And in Arch-based distributions with:

$ sudo pacman -Sy tree    [on Arch and Manjaro Linux]

Verify if it has been installed by calling:

$ tree -v

Recursive Directory Listing in Linux

The tree command shows directory listing in a neater format with proper indentations for subdirectories and different color codes for files and folders. Call it without any arguments to get the whole tree structure beneath a folder.

$ tree
Recursive Directory Listing
Recursive Directory Listing

Similarly, you can also pass a directory path as an argument to display the tree structure beneath that directory instead of the current directory.

$ tree /etc/pki
View Directory Tree Structure In Linux
View Directory Tree Structure In Linux

You can also call it with multiple directory path arguments. As seen in both the screenshots above, the command will go to the very bottom of the tree structure of a directory, to the point where there are no more subdirectories, and display everything.

Limit Depth of Recursive File Listing

However, as discussed in the beginning, this obviously becomes tricky when used on a folder with a number of subdirectories having large depth. To limit the depth of the recursive file listing use the -L argument as shown.

$ tree -L Depth Folder_Path

Here ‘Depth‘ is a positive integer indicating the maximum depth of folder structure to be displayed.

$ tree -L 2 /snap
Limit Depth of Recursive File Listing
Limit Depth of Recursive File Listing

It has restricted the display to 2 levels below the directory path passed to it, i.e., ‘/snap/‘. Without this argument, it would create a huge display as ‘snap’ is a system folder that runs very deep and contains a large number of files.

Set Different Depth Limits for Directory Listing

Similarly, you can call tree and pass multiple directory paths simultaneously with different depth limits for each:

$ tree -L 2 /home -L 3 /etc/apt/apt.conf.d
Set Different Depth Limits for Directory Listing
Set Different Depth Limits for Directory Listing

In this article, we learned about the command tree command and how to limit its depth on displayed file structure. Make sure you read the manual page of ‘tree’ to learn more about it.

Are you familiar with more similar commands? Let us know in the comments below!

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.