DEV Community

Cover image for tree in Linux
NaveenKumar Namachivayam ⚡
NaveenKumar Namachivayam ⚡

Posted on

tree in Linux

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
Enter fullscreen mode Exit fullscreen mode

To install tree in CentOS:

sudo yum install tree
Enter fullscreen mode Exit fullscreen mode

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.

Tree

Man page

man tree
Enter fullscreen mode Exit fullscreen mode

List all contents including hidden files and file starts with .

tree -a
Enter fullscreen mode Exit fullscreen mode

List all directories recursively

tree -d
Enter fullscreen mode Exit fullscreen mode

Print file patch prefix

tree -f
Enter fullscreen mode Exit fullscreen mode

Print details by max display depth

tree -L 1
Enter fullscreen mode Exit fullscreen mode

Display Depth 1

Print details by pattern

tree -P b*
Enter fullscreen mode Exit fullscreen mode

Print details by pattern

tree backups -P 'SMTP*'
Enter fullscreen mode Exit fullscreen mode

Print details by pattern
'*' - 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*'
Enter fullscreen mode Exit fullscreen mode

Print details not matching the pattern

--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.

--matchdirs and --prune

--noreport

To not to print the file and directory report, use --noreport.

tree bin -P '*.log' --prune --noreport 
Enter fullscreen mode Exit fullscreen mode

JSON Output

tree bin -P '*.log' --prune -J
Enter fullscreen mode Exit fullscreen mode

XML Output

tree bin -P '*.log' --prune -X
Enter fullscreen mode Exit fullscreen mode

Print last modification date/time

tree bin -P '*.log' --prune -D
Enter fullscreen mode Exit fullscreen mode

Print size

tree bin/ -P '*.properties' --prune -h
Enter fullscreen mode Exit fullscreen mode

Print size

Mark directories and files

-F marks directories as * and / for files.

tree -P '*.jmx' --prune -F
Enter fullscreen mode Exit fullscreen mode

Mark directories and files

--dirsfirst

It displays directories first over files. To disable, use -U.

tree -P '*.jtl' --prune --dirsfirst
Enter fullscreen mode Exit fullscreen mode

--dirsfirst

--dirsfirst

In this post, we have covered frequently used tree commands. Please check my blog for more such tutorials.

💙 QAInsights YouTube Channel

Top comments (2)

Collapse
 
mccurcio profile image
Matt Curcio

Love tree but I have not used it as half much as you, wow!
Nice tutorial,
Thanks

Collapse
 
qainsights profile image
NaveenKumar Namachivayam ⚡

Many thanks Matt! 🙏 I use it all the time :)