tree
in Linux
tree
command helps to recursively list the directory and its details in a tree format. In this post, we will see how you can use tree
effectively to view the directory details.
tree
essentials in Linux
To install tree
in Ubuntu, Debian Linux distros.
sudo apt-get install tree
To install tree
in CentOS:
sudo yum install tree
In Windows, you can issue tree
in Windows Terminal.
Enter tree
If you enter tree
command without any arguments, it will list out everything from the current directory recursively. Suppose, if you are in your home directory, it will display everything recursively like below.
Man page
man tree
List all contents including hidden files and file starts with .
tree -a
List all directories recursively
tree -d
Print file patch prefix
tree -f
Print details by max display depth
tree -L 1
Print details by pattern
tree -P b*
tree backups -P 'SMTP*'
'*' - any zero or more characters
'?' - any single character
'[...]' - any single character listed between brackets
'[A-Z]' - for range
'[^...]'- any single character not listed in brackets
'|' - separates alternate patterns
Print details not matching the pattern
tree backups -I 'SMTP*'
--matchdirs and --prune
If you use -P
which matches both the directories and files. If the match is found for directories, then the matching is disabled for its contents. Use it with --prune to prune it.
--noreport
To not to print the file and directory report, use --noreport
.
tree bin -P '*.log' --prune --noreport
JSON Output
tree bin -P '*.log' --prune -J
XML Output
tree bin -P '*.log' --prune -X
Print last modification date/time
tree bin -P '*.log' --prune -D
Print size
tree bin/ -P '*.properties' --prune -h
Mark directories and files
-F
marks directories as *
and /
for files.
tree -P '*.jmx' --prune -F
--dirsfirst
It displays directories first over files. To disable, use -U
.
tree -P '*.jtl' --prune --dirsfirst
In this post, we have covered frequently used tree commands. Please check my blog for more such tutorials.
Top comments (2)
Love
tree
but I have not used it as half much as you, wow!Nice tutorial,
Thanks
Many thanks Matt! 🙏 I use it all the time :)