- Linux ‘tree Command’ Usage Examples for Beginners
- Learn tree Command Usage Examples
- tree Command Examples in Linux
- Install tree
- List only directories
- List hidden files
- Include the path of files
- List files and directories based on the level
- List files with permission
- Get the file size of a directory using the tree command
- List files based on the last modification using the tree command
- Sort files based on first modification
- Sort files based on the last modification
- Wrapping up
- Tree in linux command
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
Linux ‘tree Command’ Usage Examples for Beginners
The tree is a tiny, cross-platform command-line program used to recursively list or display the content of a directory in a tree-like format. It outputs the directory paths and files in each sub-directory and a summary of a total number of sub-directories and files.
The tree program is available in Unix and Unix-like systems such as Linux, as well as DOS, Windows, and many other operating systems. It features various options for output manipulation, from file options, sorting options, to graphics options, and support for output in XML, JSON and HTML formats.
In this short article, we will show how to use the tree command with examples to recursively list the contents of a directory on a Linux system.
Learn tree Command Usage Examples
The tree command is available on all if not most Linux distributions, however, if you do not have it installed by default, use your default package manager to install it as shown.
# yum install tree #RHEL/CentOS 7 # dnf install tree #Fedora 22+ and /RHEL/CentOS 8 $ sudo apt install tree #Ubuntu/Debian # sudo zypper in tree #openSUSE
Once installed, you can proceed further to learn the tree command usage with examples as shown below.
1. To list directory content in a tree-like format, navigate to the directory you want and run tree command without any options or arguments as follows. Remember to invoke sudo to run the tree in a directory that requires root user access permissions.
It will display the contents of the working directory recursively showing sub-directories and files, and a summary of the total number of sub-directories and files. You can enable the printing of hidden files using the -a flag.
2. To list the directory contents with the full path prefix for each sub-directory and file, use the -f as shown.
3. You can also instruct tree to only print the subdirectories minus the files in them using the -d option. If used together with the -f option, the tree will print the full directory path as shown.
$ sudo tree -d OR $ sudo tree -df
4. You can specify the maximum display depth of the directory tree using the -L option. For example, if you want a depth of 2, run the following command.
Here is another example about setting maximum display depth of the directory tree to 3.
5. To display only those files that match the wild-card pattern, use the -P flag and specify your pattern. In this example, the command will only list files that match cata* , so files such as Catalina.sh, catalina.bat, etc. will be listed.
6. You can also tell the tree to prune empty directories from the output by adding the —prune option, as shown.
7. There are also some useful file options supported by tree such as -p which prints the file type and permissions for each file in a similar way as the ls -l command.
8. Besides, to print the username (or UID if no username is available), of each file, use the -u option, and the -g option prints the group name (or GID if no group name is available). You can combine the -p , -u and -g options to do a long listing similar to ls -l command.
9. You can also print the size of each file in bytes along with the name using the -s option. To print the size of each file but in a more human-readable format, use the -h flag and specify a size letter for kilobytes (K), megabytes (M), gigabytes (G), terabytes (T), etc..
$ sudo tree -f -s OR $ sudo tree -f -h
10. To display the date of the last modification time for each sub-directory or file, use the -D options as follows.
11. Another useful option is —du , which reports the size of each sub-directory as the accumulation of sizes of all its files and subdirectories (and their files, and so on).
12. Last but not least, you can send or redirect the tree’s output to filename for later analysis using the -o option.
$ sudo tree -o direc_tree.txt
That’s all with the tree command, run man tree to know more usage and options. If you have any questions or thoughts to share, use the feedback form below to reach us.
tree Command Examples in Linux
The tree command is excellent for viewing the structure of a directory and its subdirectories in Linux. Learn how to use it with practical examples.
You are already familiar with the directory structure in Linux. It is like the roots of a tree.
And that’s exactly the concept of the tree command. It shows the contents of the current directory and its subdirectories in a tree like fashion.
Before showing the examples of the tree command, let me quickly discuss how to install it.
Install tree
The tree utility does not come pre-installed in most Linux distributions. But you can find it in the official repositories and easily install it.
Once done, all you need to do is append the directory or directory path to the tree command and it will show file contents in a tree manner:
But the tree command can do more than just listing files in Linux. Here are some handful examples.
List only directories
If you want to list only the directories at the specified location, you can use the -d option.
List hidden files
By default, the tree command won’t list hidden files but this behavior can be altered by using the -a option.
And here, I have compared how it differs when used alongside the default manner (without any options):
Include the path of files
If you want to get the path for each file, all you need to do is use the -f option and it gets you the path of every file:
But what about getting the full path? Well, you just have to append the full path of a directory (from home to the target one) as shown:
tree -f /home/sagar/Directory
And it is lengthy. So let me share a neat way.
In this case, you can utilize the value pwd that holds the value full path. So change the directory where you want to use the tree command.
And use the following command and it will get you the full path of every file present in that directory:
List files and directories based on the level
So if the directory has hundreds of subdirectories and if you only want to list the first certain levels such as want to include the first one or two subdirectories, you can use this feature.
You can specify the levels by appending the level (in numbers) to the -L option:
For example, here, I listed files that are at level 2:
And you can see the difference where on the left side, it showed every file present whereas, on the right side (where I used level 2), it only brought the files & directories placed at level 2.
List files with permission
To attach file permissions with every file, all you need to do is append the -p option:
But to make it convenient, I would suggest pairing it with -h option for better readability.
For example, Here I went with listing every file present in the MUSIC directory:
Get the file size of a directory using the tree command
The tree command can show you the size of each file and directory at a specified location and will also sum the size for you in the end.
For that, you will have to use —df option and I would recommend pairing it with the -h for better readability:
tree --df -h TargetDirectory
List files based on the last modification using the tree command
There are two ways of sorting files based on modification:
Sort files based on first modification
To sort files based on modification, you will have to use the -c option and it will sort files that were modified the first by default.
And here, I will also be using the -D option to get the date and time of modification:
Sort files based on the last modification
In this case, you will have to alter the default effect of the -c option which will list files based on the first modification.
Which can easily be altered when paired with the -r option which will reverse the effect of -c option:
And you have them sorted for you!
Wrapping up
You can see how easy it is to visualize a nested directory structure with the tree command.
Ever since I installed it first, it’s been an essential part of my Linux command arsenal. I hope you also start using it extensively.
Tree in linux command
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter