There are few ways through which we can list files and directories in the Linux Terminal. One command is extremely common to list directories, and that command is ls command. But listing files through ls has limitations, it cannot display the structure in a tree like structure. For example :
This is where the tree command comes in.
Installing the Tree command
In some distributions, the tree command comes pre-installed. Therefore, we first have to check if the command is installed on your distribution. Open a terminal and type :
If the output shows the version of tree command, then it is already installed on your system, you can skip to the next section of this article. If the command is not installed, you can type the following command, depending upon the flavor of your OS :
For Debian and Ubuntu-based systems :
sudo apt install tree
And if you’re on Arch Linux and Arch based distributions :
sudo pacman -S tree
On Fedora workstation type :
sudo dnf install tree
How To Use the Limit the Depth of Tree Command
Simply type tree or tree <directory path> and this command will give you all the files and directories of the current or specified.
Now, let’s say we only want to go up to depth 4. To do that, we will use -L option. For example,
tree -L 4 /etc
You can also set multiple depths for different directories in the command line by using the -L option like this :
tree -L 2 /etc/sysconfig -L 3 /etc/java
Read more about the Tree command.
We hope you were able to learn more about the tree command using this article. To learn more about this command’s usage, you can refer to the man page of this command.