The tree command in Linux

We continue our series of tutorials on Linux commands. In this post, it is the turn of the tree command. This easy to use command can help us when working on the system.

Introduction to this command

The tree command is very explicit in its name. It allows us to display the contents of a Linux directory in the form of a tree.

This is very useful because although the ls command is the default for viewing files, it does not have a way to make it more practical.

It is there, where the tree command gives us a solid alternative to better visualize a directory structure.

So let’s go for it.

Install the tree command in Linux

Although the tree command is quite well known in Linux, it is not installed in all distributions. So to make sure everything goes well, we have to install it.

So, open a terminal from the main menu of your desktop and run the command that suits your distribution.

In case you are using Debian, Ubuntu or some derivative of these like Linux Mint or Elementary OS

sudo apt update
sudo apt install tree

If you use RHEL, Rocky Linux, Alma Linux, Fedora, or some derivative of these

sudo dnf install tree

And in the case of Arch Linux, Manjaro or derivatives of them

sudo pacman -S tree

In any case, the installation should not take more than a few seconds because it is a very light utility.

Using the tree command in Linux

If you use the tree command just like that, it will show you the directory structure of the current location of the prompt.

tree

You can also specify the directory to display, for example.

tree ~/Pictures

If you want the tree command to display only directories, use the following option:

tree -d

Similar to the ls command, you can show all files including the hidden ones by adding the -a option.

tree -a

You can also improve the on-screen output by having tree indicate the location of each file displayed.

tree -f

Another important option is -L which allows you to specify the level of descent in the subdirectories. Ideal for limiting screen output.

tree -L 4 ~/Pictures

Or specify a file type with the -P option. For example, to display all text files in the current directory.

tree -P *.txt

If you want to know the permissions of the files, then the -p option is the way to go.

tree -p

Finally, we can also export the screen output to an HTML file like this.

tree -H -o file.html

Finally, this simple to use command allows us to visualize the folder contents in tree form easily.

Conclusion

In this post, we have shown you how to install and use the tree command, with which you can display directory structures in tree form. I hope you liked the post.

More info

Share This:

Leave a Comment

Your email address will not be published.