The tree is a command which is available for both Linux and Windows. It recursively displays the content of directories on your system. If this command is used without an argument it will recursively print the files and directories under the current working directory. Otherwise, it takes a directory name or path as an argument and displays its content.
In this article, you will see different usage of tree command in Linux along with some examples.
How to install the tree program in Linux
Many distributions do not include this program by default so in this case first, you need to download and install it on your system. You can use one of the given commands to install it on your system –
To install it on Debian/Ubuntu/Linux Mint use –
sudo apt install tree
To install it on RHEL/CentOS use –
sudo yum install tree
On Fedora use –
sudo dnf install tree
How to use tree command in Linux
The basic syntax of using tree command on a Linux system is given below –
tree [options] [directory_path]
Where you can find the options on the man page of the tree command. And directory path is the full path of the directory whose content is to be displayed.
Usage of tree command in Linux
The following examples show the usage of the tree command in Linux.
How to display the content of the current working directory
When you run the tree command without any arguments it will display the content of the current working directory in a tree-like format. Use the given command to display the content –
or use the given command if it requires root privileges –
you can see the output in the given image –
How to display hidden files and directories in tree format
By default, the tree command does not display the hidden files or directories but you can use the option
-a with this command to display them. Now see the command below –
The files or directories that are preceded by a dot are hidden files and directories. You can see them in the image below –
How to display directories only in the tree view
If you want to display directories only in the output of the tree command you can do this by using the option
-d with the tree command. Now see the full command which is given below –
This will display directories only as you can see in the given image –
How to list files and directories with their permission
By using option
-p you can display the files and directories with permission they have. You can see the example below –
Now see the output of this command in the given image –
How to print files or directories with complete path prefix
You can use option
-f to show the full path of files or directories displayed in the output of the tree command.
You can see the output in the given image –
Display the result with the combination of less or more commands
By using less or more command with tree command make the output more readable. It is useful when you want to display the contents of the directory that contains a lot of files and directories.
For example –
tree / | less
Now you can see the output by scrolling it up and down.
Here we have discussed a few usages of the tree command it provides a lot of other features. Now if you have a query you can leave it in the comments below.