How to Use the Tree Command to Navigate File Directory in Linux

How To Use The Tree Command In Linux Feature Image

The tree command is perfect for viewing your entire directory structure at a glance. It shows folders and files in a clear, tree-like layout right in the terminal. You can control how deep it goes, include hidden files, and even save the output to a file. In this post, we’ll dive deep into how to make the most of the tree command.

Note: do not confuse the tree command with pstree. The tree command deals with file directory, while pstree deals with listing running processes.

Installation and Basic Syntax

Tree isn’t installed by default on every Linux distro, but it’s tiny and quick to add. You just need to use your distribution’s package manager. For example, on Debian/Ubuntu, use this:

sudo apt install tree
Installing Tree Command Line Tool On Linux

For those of you on Fedora, CentOS, or RHEL systems, you’ll want to use:

sudo dnf install tree

Arch Linux users can use this:

sudo pacman -S tree

Once installed, the simplest use is:

tree
Displaying Tree Like Structure By Running Tree Command

That prints the tree of the current directory, showing files and directories with connecting lines and a final summary, like the number of directories and files. By default, it lists everything recursively, displaying files and directories in a hierarchical view.

Further, you can use special flags that change how the tree displays its output, such as controlling depth, showing file sizes, etc. You can also specify the path of the folder you want to map out.

tree [options] [directory]

By default, the tree color-codes its output to make it even easier to read. Blue (or bold) represents directories, green specifies executable files (like scripts or programs), and white/default represents regular files, and so on.

You can also check what’s inside any specific directory. For example, if you’re in your home directory and want to see your Documents folder, simply type:

tree ~/Documents
Displaying Directory Structure Of Specfic Documents Using Tree

The output will cascade down your screen, showing you the complete hierarchy.

Controlling Directory Depth

Sometimes you don’t want the full infinite descent into subfolders. Maybe you only care about the top two levels, or you only need a clean overview instead of too much depth. To get a specific level, you can use the -L (level) flag of the tree command. For example, to show only two levels, you can use this:

tree -L 2
Displaying Tree Like Structure Using Tree Of Two Levels

You can also combine it with other flags for more power. For instance, you can combine -L with the -d option to show only directories and ignore files, but up to the third level or depth:

tree -d -L 3

Without limits, it can go infinitely, which is fine for small setups but overwhelming for large directories.

Including Hidden Files and Folders

In the Linux world, any file or directory that starts with a dot (.) is considered hidden. These aren’t usually for day-to-day use. They’re often configuration files or metadata folders, like “.bashrc”, “.profile”, or the common “.git” directory in Git repositories. The tree command by default hides or ignores these hidden files and folders.

However, you can include them with the -a flag:

tree -a

Now, you get every directory, including all the important hidden configuration files. If you want hidden files plus depth control, use this:

tree -a -L 2
Showing Files Along With Hidden Files With Two Levels

Displaying Files by Matching Patterns

Sometimes you don’t want to see everything; you’re hunting for something specific. Maybe you only care about Python files, or you’re looking for all your Markdown documents. The -P option lets you specify a pattern to match.

For instance, if you want to see only Python files, run:

tree -P "*.py"

If you are looking for all your text files, use:

tree -P "*.txt"

You can also use wildcards and get creative with your patterns. Let’s say you need to find all files that start with config; you can do that with:

tree -P "config*"

When you use -P, the tree will still show the directory structure, but it’ll only display files that match your pattern. Empty directories, ones that don’t contain matching files, will still appear in the output. If you find this cluttering, you can use the --prune option:

tree -P "*.py" --prune

This removes any empty directories from the output, giving you a cleaner view that shows only the paths leading to your target files. One more thing: if you want to exclude names that match a pattern, use -I instead of -P.

Showing File Sizes in Human-Readable Format

To be honest, for me, seeing file sizes in bytes isn’t particularly helpful. When a file is 524288 bytes, I have to stop and do mental maths to figure out that’s about 512 KB. Fortunately, the -h option solves this beautifully. It displays file sizes in a human-readable format, the way we actually think about file sizes. For example:

tree -h
Getting Human Readable File Sizes Using Tree

Now, instead of seeing 524288, you’ll see 512K. A file that’s 1048576 bytes becomes 1.0M. This is useful when you’re eager to find out where all your disk space went. You can quickly scan through the tree and spot suspiciously large files.

Here’s another combination I use frequently:

tree -hL 2

This shows me two levels deep, with human-readable file sizes.

Combining tree With Other Commands

In Linux, the real power isn’t in any single command, but in how you can combine them. The tree command just outputs text. And we can pipe it to other commands and perform various operations on it. The pipe (|) takes the output from the command on the left and sends it as input to the command on the right.

Let’s say you want to run a tree on a big directory, but you don’t want it to fly by your screen for 30 seconds. You intend to be able to scroll through it, search it, and read it at your own pace. You can do this by combining tree with the less command:

tree /usr/lib | less

Run this, and your screen will lock onto the first page of the tree output.

Scrollable First Page Of Tree Command

You can now use the arrow keys (Up/Down), spacebar to go down a full page, / followed by a word to search the entire tree for that word, and q to quit and return to your prompt.

Further, the -P flag is great for simple patterns, but what if you seek more? What if you want to find any file or directory that has the word admin in it, case-insensitive? This is a job for grep, try combining it with tree like this:

tree -a | grep -i "admin"

You can also find every line containing config in the tree by pairing the wc command with the tree like this:

tree -a | wc -l

This gives an approximate count of lines.

Saving or Exporting tree Output

Sometimes you need to save that tree structure for later. Maybe you’re documenting a project, creating a report, or just want to keep a snapshot of your directory structure before making changes. The simplest old way to do it is using redirection like this:

tree > directory_structure.txt

This saves the entire tree output to a text file. Simple, effective, and you can open it in any text editor.

You can also create an HTML page with clickable links and collapsible sections by using the built-in -H option:

tree -H . > structure.html

Open it in a browser, and you’ve got an interactive view of your directory structure.

For documentation purposes, you can also save the output with specific formatting:

tree -L 3 -a --dirsfirst > project_docs.txt

The --dirsfirst option lists directories before files, making the output even more organized. This is perfect for README files or project documentation.

Finally, you can also append to existing files if you’re building a larger document:

tree -L 2 >> documentation.txt

This adds the tree output to the end of your existing documentation file without overwriting what’s already there.

Final Thoughts

This is the basic use of the tree command. It offers many other options you can explore. To learn more, type info tree in the terminal or check the manual online. Press q to exit when you’re done. You can also explore other useful commands like ls, find, and du to manage and view your files in different ways.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Haroon Javed Avatar