How to use the tree command in Linux
The tree
command in Linux allows you to recursively traverse and list all the files and directories in the specified path. The command will use a depth-indented format showing the location of files and directories within the filesystem.
If you want to quickly search and locate a specific file or directory, the tree
command is probably the best tool for it.
Using this tutorial, you will learn how you can use the tree
command to show directory structure in a simple and intuitive format.
Installing the tree
Utility
In most cases, the tree
command is readily available in most Linux distributions. However, if you are using a minimal Linux instance or you get the tree command not found
error, you need to install it.
For Debian/Ubuntu and other Debian based distributions, run the command:
sudo apt-get update
sudo apt-get install tree
On REHL/CentOS/Fedora:
sudo yum install tree
On Arch/Manjaro:
sudo pacman -S tree
The tree
command Syntax
The tree
command offers a simple syntax that enables you to quickly list all and directories in a target location.
The command syntax is as shown:
tree [Option] path_to_target_directory
The command accepts two main arguments:
Option
- specifies the target option you wish to apply in the command. This is otional.path_to_target_directory
- sets the path to the directory whose files and subdirectories listing you wish to see. If this parameter is not specified the command will list the files in your current working directory.
tree
command options
The following are some of common and useful options you can use with the tree
command.
Listing Options
These options are used to modify how the files and subdirectories are listed:
-a All files are listed.
-d List directories only.
-l Follow symbolic links like directories.
-f Print the full path prefix for each file.
-x Stay on current filesystem only.
-L level Descend only level directories deep.
-R Rerun tree when max dir level reached.
-P pattern List only those files that match the pattern given.
-I pattern Do not list files that match the given pattern.
--ignore-case Ignore case when pattern matching.
--matchdirs Include directory names in -P pattern matching.
--noreport Turn off file/directory count at end of tree listing.
--charset X Use charset X for terminal/HTML and indentation line output.
--filelimit # Do not descend dirs with more than # files in them.
--timefmt <f> Print and format time according to the format <f>.
-o filename Output to file instead of stdout.
File Options
Governs the file listing options:
-q Print non-printable characters as '?'.
-N Print non-printable characters as is.
-Q Quote filenames with double quotes.
-p Print the protections for each file.
-u Displays file owner or UID number.
-g Displays file group owner or GID number.
-s Print the size in bytes of each file.
-h Print the size in a more human readable way.
--si Like -h, but use in SI units (powers of 1000).
-D Print the date of last modification or (-c) status change.
-F Appends '/', '=', '*', '@', '|' or '>' as per ls -F.
--inodes Print inode number of each file.
--device Print device ID number to which each file belongs.
Sorting Options
Defines the sorting behavior.
-v Sort files alphanumerically by version.
-t Sort files by last modification time.
-c Sort files by last status change time.
-U Leave files unsorted.
-r Reverse the order of the sort.
--dirsfirst List directories before files (-U disables).
Input Options
--fromfile Reads paths from files (.=stdin)
Graphics/Display Options
-i Don't print indentation lines.
-A Print ANSI lines graphic indentation lines.
-S Print with CP437 (console) graphics indentation lines.
-n Turn colorization off always (-C overrides).
-C Turn colorization on always.
To view all the available command options, run:
tree --help
Show Tree Listing in Current Working Directory
To show the listing of files and directories in your cwd, run the tree
command with no arguments:
tree
The command will print an output as shown:
/var/log$ tree
.
├── alternatives.log
├── apt
│ ├── eipp.log.xz
│ ├── history.log
│ └── term.log
├── bootstrap.log
├── btmp
├── cassandra
│ ├── debug.log
│ ├── debug.log.2022-07-30.0.zip
│ ├── gc.log
│ ├── gc.log.0
│ ├── gc.log.1
│ ├── gc.log.2
│ ├── system.log
│ └── system.log.2022-07-30.0.zip
├── dpkg.log
├── faillog
├── journal
├── lastlog
├── ntpstats
└── wtmp
4 directories, 18 files
Keep in mind that this output may vary depending on your current directory.
Store output to file.
You can store the output of the tree
command by using the redirection operator. For example, to store the output to a file called var_log.txt
:
tree > var_log.txt
Show Hidden Files and Directories
By default, the tree
will not display hidden files and directories. However, you can use the -a
option to show them.
tree -a
Output:
.
├── .bash_history
├── .bash_logout
├── .bashrc
├── .cassandra
├── Desktop
├── .local
│ └── lib
│ └── python3.9
│ └── site-packages
├── .profile
├── .python_history
└── var_log.txt
6 directories, 6 files
In this case, we can see the directory output including hidden files and directories.
Tree Pattern Matching
The tree
command provides us with a set of wildcard characters we can use to search for files and directories matching specific directories.
The supported wildcard characters include:
*
- match zero or more characters.?
- match any single character.[...]
- match any single character available in the brackets.[A-Z]
- specifies a character range.[^...]
- match any single character not available in the brackets.|
- specifies multiple patterns separated by the pipe operator.
Show Files Matching a Specific Pattern
To show only files and directories that match a specific pattern, we can use the -P
flag as shown in the command below:
sudo tree -P "*.conf"
The command will only list the files and directories with .conf
pattern in the name.
An example output is as shown:
├── adduser.conf
├── alternatives
├── apache2
│ └── conf-available
│ └── javascript-common.conf
├── apparmor
│ └── init
│ └── network-interface-security
├── apparmor.d
│ ├── local
│ └── tunables
├── apt
│ ├── apt.conf.d
│ ├── auth.conf.d
│ ├── preferences.d
│ ├── sources.list.d
│ └── trusted.gpg.d
├── binfmt.d
├── ca-certificates
│ └── update.d
├── ca-certificates.conf
├── cassandra
│ └── triggers
├── cron.d
├── cron.daily
├── cron.hourly
├── cron.monthly
├── cron.weekly
├── dbus-1
│ ├── session.d
│ └── system.d
├── debconf.conf
├── default
├── deluser.conf
│ │ ├── 40-nonlatin.conf -> /usr/share/fontconfig/conf.avail/40-nonlatin.conf
│ │ ├── 45-generic.conf -> /usr/share/fontconfig/conf.avail/45-generic.conf
│ │ ├── 45-latin.conf -> /usr/share/fontconfig/conf.avail/45-latin.conf
│ │ ├── 49-sansserif.conf -> /usr/share/fontconfig/conf.avail/49-sansserif.conf
│ │ ├── 50-user.conf -> /usr/share/fontconfig/conf.avail/50-user.conf
│ │ ├── 51-local.conf -> /usr/share/fontconfig/conf.avail/51-local.conf
│ │ ├── 57-dejavu-sans.conf -> ../conf.avail/57-dejavu-sans.conf
│ │ ├── 57-dejavu-sans-mono.conf -> ../conf.avail/57-dejavu-sans-mono.conf
│ │ ├── 57-dejavu-serif.conf -> ../conf.avail/57-dejavu-serif.conf
│ │ ├── 58-dejavu-lgc-sans.conf -> ../conf.avail/58-dejavu-lgc-sans.conf
│ │ ├── 58-dejavu-lgc-sans-mono.conf -> ../conf.avail/58-dejavu-lgc-sans-mono.conf
│ │ ├── 58-dejavu-lgc-serif.conf -> ../conf.avail/58-dejavu-lgc-serif.conf
│ │ ├── 60-generic.conf -> /usr/share/fontconfig/conf.avail/60-generic.conf
│ │ ├── 60-latin.conf -> /usr/share/fontconfig/conf.avail/60-latin.conf
│ │ ├── 65-fonts-persian.conf -> /usr/share/fontconfig/conf.avail/65-fonts-persian.conf
│ │ ├── 65-nonlatin.conf -> /usr/share/fontconfig/conf.avail/65-nonlatin.conf
│ │ ├── 69-unifont.conf -> /usr/share/fontconfig/conf.avail/69-unifont.conf
│ │ ├── 70-no-bitmaps.conf -> /usr/share/fontconfig/conf.avail/70-no-bitmaps.conf
│ │ ├── 80-delicious.conf -> /usr/share/fontconfig/conf.avail/80-delicious.conf
│ │ └── 90-synthetic.conf -> /usr/share/fontconfig/conf.avail/90-synthetic.conf
│ └── fonts.conf
├── gai.conf
├── groff
├── gss
│ └── mech.d
├── host.conf
├── init.d
├── iproute2
│ ├── rt_protos.d
│ └── rt_tables.d
├── java-11-openjdk
│ ├── jfr
│ ├── management
│ └── security
│ └── policy
│ ├── limited
│ └── unlimited
├── kernel
├── rc0.d
├── rc1.d
├── rc2.d
├── rc3.d
├── rc4.d
├── rc5.d
├── rc6.d
├── rcS.d
├── resolv.conf -> /mnt/wsl/resolv.conf
├── rsyslog.conf
├── rsyslog.d
├── security
│ ├── access.conf
│ ├── faillock.conf
│ ├── group.conf
│ ├── limits.conf
│ ├── limits.d
│ │ └── cassandra.conf
│ ├── namespace.conf
│ ├── namespace.d
│ ├── pam_env.conf
│ ├── sepermit.conf
│ └── time.conf
├── selinux
│ └── semanage.conf
├── sensors3.conf
├── sensors.d
├── skel
├── ssl
│ ├── certs
│ │ └── java
│ └── private
├── sudo.conf
├── sudoers.d
├── sudo_logsrvd.conf
├── sysctl.conf
└── xdg
├── autostart
├── systemd
└── user-dirs.conf
133 directories, 100 files
NOTE: The above output has been truncated.
Show Files that do not match a patter
To show the files that do not match a specific pattern, you can use the -I
option as shown:
sudo tree -I "*.conf"
In this case, the command will list all the files that do not match the set pattern.
.
├── alternatives
│ ├── awk -> /usr/bin/mawk
│ ├── awk.1.gz -> /usr/share/man/man1/mawk.1.gz
│ ├── builtins.7.gz -> /usr/share/man/man7/bash-builtins.7.gz
│ ├── c++ -> /usr/bin/g++
│ ├── c89 -> /usr/bin/c89-gcc
│ ├── c89.1.gz -> /usr/share/man/man1/c89-gcc.1.gz
│ ├── c99 -> /usr/bin/c99-gcc
│ ├── c99.1.gz -> /usr/share/man/man1/c99-gcc.1.gz
│ ├── cc -> /usr/bin/gcc
│ ├── cpp -> /usr/bin/cpp
│ ├── editor -> /bin/nano
│ ├── editor.1.gz -> /usr/share/man/man1/nano.1.gz
│ ├── ex -> /usr/bin/vim.tiny
│ ├── ex.1.gz -> /usr/share/man/man1/vim.1.gz
│ ├── ex.da.1.gz -> /usr/share/man/da/man1/vim.1.gz
│ ├── ex.de.1.gz -> /usr/share/man/de/man1/vim.1.gz
│ ├── ex.fr.1.gz -> /usr/share/man/fr/man1/vim.1.gz
│ ├── ex.it.1.gz -> /usr/share/man/it/man1/vim.1.gz
│ ├── ex.ja.1.gz -> /usr/share/man/ja/man1/vim.1.gz
│ ├── ex.pl.1.gz -> /usr/share/man/pl/man1/vim.1.gz
│ ├── ex.ru.1.gz -> /usr/share/man/ru/man1/vim.1.gz
│ ├── faked.1.gz -> /usr/share/man/man1/faked-sysv.1.gz
│ ├── faked.es.1.gz -> /usr/share/man/es/man1/faked-sysv.1.gz
│ ├── faked.fr.1.gz -> /usr/share/man/fr/man1/faked-sysv.1.gz
│ ├── faked.sv.1.gz -> /usr/share/man/sv/man1/faked-sysv.1.gz
│ ├── fakeroot -> /usr/bin/fakeroot-sysv
│ ├── fakeroot.1.gz -> /usr/share/man/man1/fakeroot-sysv.1.gz
│ ├── fakeroot.es.1.gz -> /usr/share/man/es/man1/fakeroot-sysv.1.gz
│ ├── fakeroot.fr.1.gz -> /usr/share/man/fr/man1/fakeroot-sysv.1.gz
│ ├── fakeroot.sv.1.gz -> /usr/share/man/sv/man1/fakeroot-sysv.1.gz
│ ├── jaotc -> /usr/lib/jvm/java-11-openjdk-amd64/bin/jaotc
│ ├── jar -> /usr/lib/jvm/java-11-openjdk-amd64/bin/jar
│ ├── jar.1.gz -> /usr/lib/jvm/java-11-openjdk-amd64/man/man1/jar.1.gz
│ ├── jarsigner -> /usr/lib/jvm/java-11-openjdk-amd64/bin/jarsigner
│ ├── jarsigner.1.gz -> /usr/lib/jvm/java-11-openjdk-amd64/man/man1/jarsigner.1.gz
│ ├── java -> /usr/lib/jvm/java-11-openjdk-amd64/bin/java
│ ├── java.1.gz -> /usr/lib/jvm/java-11-openjdk-amd64/man/man1/java.1.gz
Similarly, the output above has been truncated.
Show Absolute Path
In some cases, you may want to know the absolute path of a specific file or directory. You can use the tree
command with the -f
option.
sudo tree -f /var/log/
This should list all the files and directories including the absolute path as:
/var/log
├── /var/log/alternatives.log
├── /var/log/apt
│ ├── /var/log/apt/eipp.log.xz
│ ├── /var/log/apt/history.log
│ └── /var/log/apt/term.log
├── /var/log/bootstrap.log
├── /var/log/btmp
├── /var/log/cassandra
│ ├── /var/log/cassandra/debug.log
│ ├── /var/log/cassandra/debug.log.2022-07-30.0.zip
│ ├── /var/log/cassandra/gc.log
│ ├── /var/log/cassandra/gc.log.0
│ ├── /var/log/cassandra/gc.log.1
│ ├── /var/log/cassandra/gc.log.2
│ ├── /var/log/cassandra/system.log
│ └── /var/log/cassandra/system.log.2022-07-30.0.zip
├── /var/log/dpkg.log
├── /var/log/faillog
├── /var/log/journal
├── /var/log/lastlog
├── /var/log/ntpstats
└── /var/log/wtmp
4 directories, 18 files
Show File and Directory Permissions
To include the file and directory permissions in the listing, you can use the -p
option as:
sudo tree -p /var/log
Resulting output:
/var/log
├── [-rw-r--r--] alternatives.log
├── [drwxr-xr-x] apt
│ ├── [-rw-r--r--] eipp.log.xz
│ ├── [-rw-r--r--] history.log
│ └── [-rw-r-----] term.log
├── [-rw-r--r--] bootstrap.log
├── [-rw-rw----] btmp
├── [drwxr-xr-x] cassandra
│ ├── [-rw-r--r--] debug.log
│ ├── [-rw-r--r--] debug.log.2022-07-30.0.zip
│ ├── [-rw-r--r--] gc.log
│ ├── [-rw-r--r--] gc.log.0
│ ├── [-rw-r--r--] gc.log.1
│ ├── [-rw-r--r--] gc.log.2
│ ├── [-rw-r--r--] system.log
│ └── [-rw-r--r--] system.log.2022-07-30.0.zip
├── [-rw-r--r--] dpkg.log
├── [-rw-r--r--] faillog
├── [drwxr-sr-x] journal
├── [-rw-rw-r--] lastlog
├── [drwxr-xr-x] ntpstats
└── [-rw-rw-r--] wtmp
4 directories, 18 files
Sort Files and Directory Listing
You can use the various sorting parameters to sort the output. For example, to sort the files and directories based on the last modified date, we can use the -t
option as:
tree -ta
The command should list the files and directories with the last modified date:
.
├── .bash_logout
├── .bashrc
├── .profile
├── .local
│ └── lib
│ └── python3.9
│ └── site-packages
├── .python_history
├── .cassandra
├── Desktop
├── .bash_history
└── var_log.txt
6 directories, 6 files
Closing
In this tutorial, we explored the various aspects of the tree
command and how you can use it to list the files and directories within your filesystem. The tree is a comprehensive command and requires practice. Use the tree --help
command to explore the supported options.
If you enjoyed this tutorial, feel free to share and leave us a comment below.