Seeing the forest with tree
In Full View

The tree command can provide a clear view of all your files and directories and offers a few advantages over the usual ls command.
The tree
command [1] is a tool that I'd overlooked until recently. After trying it for several weeks, however, I am starting to prefer its tree-like display of directories and files to the plain lists generated by ls
.
Part of the reason is that, in these GUI-oriented days, even a directory tree made with ASCII characters is more comfortable than no structural display at all. The functional differences between tree
and ls
are minimal. The two commands share a few options, such as -a
for displaying all files, and even when the options are different, the functionality remains similar. The largest exception I have noticed is that ls
offers an option for the number of columns in which to display results; the tree
display makes a single column unavoidable.
You have to look closely to see the differences, but tree
is consistently more versatile, starting with, unlike ls
, recursion. By default, tree
displays all files in all subdirectories.
Like ls
, tree is part of the standard installation for most distributions. If yours does not include it, or if you want the absolutely latest version, you can download the source code from the project site to compile.
Without any options or target directory to view, tree lists the current directory. Alternatively, you can specify a directory to be the top level in the display (Figure 1).
Unless you know that the directories you are viewing contain only a few files, you will almost certainly want to pipe it through less
, so that you can scroll back and forth in the results.
The basic command structure is:
tree OPTIONS TOP-DIRECTORY | less
However, this structure is only the beginning. Tree also includes extensive options for adjusting how results are displayed, how the command operates, how files are listed, and how files are sorted.
Display Options
If your environment includes LS_COLORS
, the display uses the same color codes as ls
(blue for directories, white for general files, red for archived files, green for an uncompiled script). One of the most obvious advantages over ls
is that an archived file is treated as subdirectory file, so you can read its files directly from tree
. The output ends with the total number of directories and files displayed.
You can modify this default display in several ways. Adding the -n
option after the basic command turns colorization off, whereas -C
restores it. Using -i
suppresses indentation for directories, giving similar results to ls
in a single column, and --noreport
removes the final count of directories and files.
Unmodified, tree
does not include the last time a file or directory was modified. However, you add the information with --timefmt FORMAT
. The --timefmt
option uses the formats defined in strftime
[2], a standard convention for displaying date and time. Strftime includes dozens of options, but %c
(the default time and date for the current system's locale) or %d/%m/%y
(day/month/year) are two of the most useful for the date, and %T
(for a 24-hour clock) for time. If none of these options are what you want, consult the strftime
man page for a complete list of options.
Operational Options
By default, tree
displays all non-hidden files and directories below the directory you specify. However, you can modify how tree
handles different aspects of the directory tree in several ways.
To start, you can add the -a
option to list hidden files, just as you can with ls
. At the opposite extreme, -d
lists only directories. Add --prune
, and results can become sparser yet, with no empty directories displayed. Other options limit the input to tree
. Using -L LEVEL
, you can limit the directory depth to display (Figure 2). The option --filelimit NUMBER
imposes a similar restriction, displaying only directories with more than the stated number of entries. If your hard drive is partitioned with LVM or uses RAID, -x
will restrict results only tho those on the current filesystem.
The tree
command also uses basic wildcards when the -P
option is used, such as *
for zero or more characters, ?
for any single character, and a list or range of characters within square brackets (e.g., [ABC]
or [5-6]
). When the contents of the brackets begin with a ^
, then the search is for a single character not listed in the brackets.
Whatever options you use, -o=FILENAME
saves the results to a file. Tree saves in XML format by default, but if necessary, -x
will turn XML output back on. Alternatively, -H BASEHREF
turns on HTML output, which can be handy for referencing an HTML or FTP site. When you do choose HTML output, --nolinks
turns off hyperlinks in the output file, and -T TITLE
sets the title and first level header in the output file to HTML.
Many of these options can be especially useful if you have some knowledge of the directory structure and want to eliminate large segments of the structure as you search for particular files and directories. In my experience, they are best used in separate sweeps of the directory tree so that one does not interfere with another.
File Output Options
Tree gives you the choice of substituting non-printable characters in results with a question mark (-q
) or printing them as is (-N
). You also have the option of printing file names with double-quotation marks around them (-Q
), which can be convenient if a file title uses spaces and you plan to copy and paste it for use in another command (Figure 3).
Colorblind users might also prefer to include -F
in the command, to print symbols to identify file types – for example, /
for directories.
However, most of tree
's file output options display meta-information. With -u
, tree
prints the user ID (UIF), and -g
prints the group ID (GID), as shown in Figure 4. If your interest is in file sizes, then -s
prints them in bytes, and -h
shows them in the most suitable unit readable for humans: K
specifying kilobytes, M
for megabytes, G
for gigabytes, and so on.

You can also include in the output the disk usage for each directory with -du
or show the device number for each result with --device
.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.
News
-
Red Hat Migrates RHEL from Xorg to Wayland
If you've been wondering when Xorg will finally be a thing of the past, wonder no more, as Red Hat has made it clear.
-
PipeWire 1.0 Officially Released
PipeWire was created to take the place of the oft-troubled PulseAudio and has finally reached the 1.0 status as a major update with plenty of improvements and the usual bug fixes.
-
Rocky Linux 9.3 Available for Download
The latest version of the RHEL alternative is now available and brings back cloud and container images for ppc64le along with plenty of new features and fixes.
-
Ubuntu Budgie Shifts How to Tackle Wayland
Ubuntu Budgie has yet to make the switch to Wayland but with a change in approaches, they're finally on track to making it happen.
-
TUXEDO's New Ultraportable Linux Workstation Released
The TUXEDO Pulse 14 blends portability with power, thanks to the AMD Ryzen 7 7840HS CPU.
-
AlmaLinux Will No Longer Be "Just Another RHEL Clone"
With the release of AlmaLinux 9.3, the distribution will be built entirely from upstream sources.
-
elementary OS 8 Has a Big Surprise in Store
When elementary OS 8 finally arrives, it will not only be based on Ubuntu 24.04 but it will also default to Wayland for better performance and security.
-
OpenELA Releases Enterprise Linux Source Code
With Red Hat restricting the source for RHEL, it was only a matter of time before those who depended on that source struck out on their own.
-
StripedFly Malware Hiding in Plain Sight as a Cryptocurrency Miner
A rather deceptive piece of malware has infected 1 million Windows and Linux hosts since 2017.
-
Experimental Wayland Support Planned for Linux Mint 21.3
As with most Linux distributions, the migration to Wayland is in full force. While some distributions have already made the move, Linux Mint has been a bit slower to do so.