As a Linux user, our first choice for directory listings is the good old ls command. The ls command, however, lacks some features that are provided by our topic under discussion-the tree command. This command prints the folders, subfolders, and files in the form of a tree. You can make the command even more useful by using various options/flags with it to customize the listing.
This article will explain, with examples, the usage of the tree command.
We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system.
How to Install the Tree Command
Installing the tree command line utility is pretty simple through the apt-get command. Open your Ubuntu command line, the Terminal, either through the system Dash or the Ctrl+Alt+t shortcut.
Then enter the following command as sudo:
$ sudo apt-get install tree
Please note that only an authorized user can add, remove and configure software on Ubuntu.
After tree is installed, you can check the version number and also ensure if the installation was successful through the following command:
$ tree --version
We recommend running the following command before each install so that you can get the latest available version of a software present in the online repositories:
$ sudo apt-get update
How to Use Tree command
Here we will mention some examples of the tree command so that you can not only use it but also take a step forward in mastering it.
Basic Tree output
This is the most basic way of using the tree command:
The output shows a tree structure of your current directory, displaying all the folders, sub-folders and files.
Display contents of a specific directory
In order to list the files and subfolders of a specific directory rather than that of the current directory, you can specify the directory name or path through the following command syntax:
$ tree -a [DirectoryName/Path]
The following command will list all the files and sub-folders, if any, in the Pictures directory:
$ tree -a Pictures
Display hidden files along with other files using Tree
The tree command does not display the listing of hidden files and folders in Ubuntu. You can, however, use the ‘a’ flag as follows in order to list them:
$ tree -a
The files and folders in the tree starting from a ‘.’ are the hidden ones. In the above output, I have highlighted one such entry to explain how it looks like.
Display only directory listing through Tree
If you want to view only the directory listing and not the underlying files, you can use the d flag with the tree command as follows:
$ tree -d
Display full path prefix of files and folders using Tree
With the f fag, you can customize the tree flag to display the complete path as prefix for all the files and folders list.
$ tree -f
This is especially helpful when you want to know what exists where.
Display size of files and folders using Tree
With the s flag, you can make the tree command print the size, in bytes, of all the files and folders in your directory.
$ tree -s
This helps you in determining which items are taking a large amount of space on your system and getting rid of the unnecessary ones.
Display read-write permissions of files and folders using Tree
Through the p flag in your tree command, you can view the read, write and delete permissions on the listed files and folders.
$ tree -p
So before you want to perform an operation on a file and folder, you can first know and may be edit the permissions you have on a specific item.
List folder contents till a certain level/depth through Tree
Instead of listing all the contents of your directory, you can configure the tree command to display the tree to a certain level or depth. For example level 1 in the tree command will only show the list of the given folder rather than any of its subfolders. Here is how to use the syntax:
$ tree -L [n]
The following command will display only the sub-directories (with the help of -d flag) of the current directory and not the further expanded tree.
$ tree -d -L 1
Make The Tree command print file listing containing a specific pattern
You can use the tree command to only list the files containing a specific wild card pattern. Here is the syntax to specify the pattern:
$ tree -P [[pattern]*]/[*[pattern]]/[[*pattern*]]
In this example, I am using the tree command to list those files starting with the keyword “touch”:
$ tree -P touch*
Make the Tree command avoid printing some selective file names
You can also use the tree command to list everything but the files containing a specific wild card pattern.
$ tree -I *[keywords]
The following command will list all the files and folders except for the one containing the “snap” keyword.
$ tree -d -I *snap
Print Tree command output to a file
If you want to print the result of the tree command to a file, you can use the following syntax:
$ tree -o [filename]
The following command will print the list of all files and folders of the Pictures folder to an HTML file named myfile.html
$ tree ./Pictures -o myfile.html
The tree command is much more helpful than the usage we have described. You can explore the command further by viewing the help of the tree command as follows:
$ tree --help
By using the flags we described and also by using combinations of these flags, you can master the tree command even more!