Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

How to use the tree command to view directories in Linux

List Linux directories with tree, limit depth, include hidden files, filter names, display sizes, and export a readable text or HTML listing.

Table of Contents

The Linux tree command prints files and directories as a hierarchy. Use it to inspect a project's layout, locate a folder within that layout, or document a directory structure. It lists names; it does not change your working directory or open files.

tree and pstree serve different purposes: tree shows filesystem directories, while pstree shows relationships between running processes.

Install tree and list a directory

If tree is not installed, use the command for your distribution.

Debian or Ubuntu:

sudo apt install tree

How to use Tree command to navigate file directory in Linux Picture 1

Fedora and supported RHEL-based systems using DNF:

sudo dnf install tree

Arch Linux:

sudo pacman -S tree

Then list the current directory:

tree

How to use Tree command to navigate file directory in Linux Picture 2

Or supply a path:

tree ~/Documents

By default, tree descends into subdirectories, omits names beginning with a dot, and prints a count at the end. It can list only locations your account is permitted to read. Symbolic links appear as links; tree does not normally descend into linked directories unless asked to do so.

For a folder with many files, start with a depth limit rather than listing the entire filesystem. Use tree --help or man tree for the options supported by your installed version.

Limit depth and show only directories

-L sets the maximum number of levels to display below the starting directory:

tree -L 2

How to use Tree command to navigate file directory in Linux Picture 3

To display only directories, combine it with -d:

tree -d -L 3

This is useful when you want the folder structure without a long list of files. The limit controls the listing's depth, not your filesystem permissions. Omitting it allows a recursive listing within the command's other limits.

Include hidden files and directories

In Linux, a name beginning with . is treated as hidden by many listing tools. These names often include configuration files such as .bashrc and directories such as .git.

tree -a
tree -a -L 2

How to use Tree command to navigate file directory in Linux Picture 4

-a includes hidden entries, but does not bypass access restrictions. Combine it with a depth limit when checking a home directory or repository. Before sharing the output, review filenames for private information.

Filter file names or exclude unwanted folders

-P includes file names matching a pattern. Quote the pattern so the shell does not expand it before tree receives it:

tree -P '*.py'
tree -P '*.txt'
tree -P 'config*'

These match Python files, text files, and names beginning with config, respectively. The directory hierarchy remains visible, including directories without matching files. Add --prune to omit empty branches from the displayed result:

tree -P '*.py' --prune

Pruning affects the output only; it does not delete directories. To exclude names, use -I. For example, this hides Git metadata and a dependency folder while including other hidden entries:

tree -a -L 3 -I '.git|node_modules'

For searching by owner, modification time, or other file properties, use a search tool instead. See finding files in Linux for the role of find and related commands.

Display sizes without confusing them with folder totals

-h shows sizes with readable suffixes:

tree -h
tree -h -L 2

How to use Tree command to navigate file directory in Linux Picture 5

This can help identify large individual files. However, a directory's ordinary displayed size is not the sum of everything inside it. Do not use tree -h alone to decide which folder consumes the most disk space.

tree --du -h accumulates sizes through the directory hierarchy, but can take more time and memory on a large tree. For investigating storage usage, the du command is usually the more direct tool. File sizes and allocated disk space can also differ.

Read a long listing with less

Pipe the result to less to browse it a screen at a time:

tree -L 3 /usr/lib | less

How to use Tree command to navigate file directory in Linux Picture 6

Use the arrow keys to move, Space for the next screen, / followed by text to search, and q to return to the shell. The pipe sends tree's text output to the pager; it does not lock the terminal permanently.

Save text or export HTML

Redirect a listing to a text file. Writing it outside the directory being listed prevents the report itself from appearing in that listing:

tree -L 3 -a --dirsfirst . > ../directory_structure.txt

--dirsfirst lists directories before files. The destination's parent directory must be writable. A single > creates the file or overwrites an existing one; use a new filename when you need to keep an earlier report.

To append instead of replacing existing content:

tree -L 2 >> ../directory_structure.txt

-H generates HTML and uses its argument as the base for links. For a report kept beside the listed files:

tree -H . -L 2 -I 'structure.html' > structure.html

Open the resulting file in a browser. The links depend on the chosen base and where the report is stored; moving the report alone can break them. -H provides an HTML listing, and does not by itself guarantee collapsible folders. For exact version-specific behavior, consult the upstream tree manual.

A directory listing records names and structure at that moment. It is documentation, not a backup of the files or their contents.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.