How to Display Directory Structures Using the tree Command in Linux

When you are navigating a massive, complex Linux filesystem containing hundreds of nested directories and thousands of files, the standard ls command is incredibly inefficient. It only displays the exact folder you are currently standing inside. If you want to see what is hidden three folders deep, you have to manually run cd and ls over and over again, completely losing track of the overarching architectural structure. To force the kernel to instantly map the entire directory layout and render it as a massive, highly visual hierarchy, you must use the tree command.

How the tree Command Works

Unlike ls, the tree command is a recursive mapping engine. When you execute it, it does not stop at the current directory. It aggressively dives into every single sub-folder, indexes every file, and mathematically draws a perfect, branched visual map on your terminal screen using standard ASCII characters.

To execute a basic map of your current directory, simply type the command:

tree

The terminal will instantly output a visual hierarchy:

.
├── documents
│   ├── invoice_q1.pdf
│   └── invoice_q2.pdf
├── images
│   ├── logo.png
│   └── background.jpg
└── scripts
    └── backup.sh

3 directories, 5 files

Filtering the Visual Map

If you run the tree command at the absolute top of the Linux filesystem (the root / directory), it will attempt to map millions of files, crashing your terminal output in an endless loop of scrolling text. You must use flags to strictly limit its depth and scope.

To force the engine to map the structure but completely ignore all the individual files (only showing the nested folders themselves), use the -d (directories only) flag:

tree -d /var/www/html/

To explicitly prevent the engine from diving too deep into the architecture, use the -L (Level) flag followed by a strict numerical limit. For example, to map the directory but absolutely force the engine to stop indexing after 2 levels deep, execute:

tree -L 2 /var/log/

This generates a clean, high-level architectural overview without flooding your screen with irrelevant, deeply buried text files.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.