Apps Artificial Intelligence CSS DevOps Go JavaScript Laravel Linux MongoDB MySQL PHP Python Rust Svelte Vue

Display a Directory Tree on Linux

1 min read .
Display a Directory Tree on Linux

A directory tree makes project and filesystem structure easier to understand than a flat list of paths. The dedicated tree utility is usually the clearest tool for this job.

Use tree

tree

Example output:

.
└── folder1
    ├── subfolder1
    │   └── file1.txt
    └── subfolder2
        └── file2.txt

Include hidden entries:

tree -a

Limit recursion depth:

tree -L 2 /path/to/directory

Show directories only:

tree -d

Install the package if necessary:

sudo apt install tree   # Debian/Ubuntu
sudo dnf install tree   # Fedora/RHEL-family systems where available

Use find When tree Is Unavailable

A plain recursive path listing can be generated with:

find /path/to/directory -print

GNU find can also format depth information for custom reports, but reproducing a full tree drawing correctly in shell code is more complicated than it first appears.

Avoid Parsing Recursive ls for Automation

Pipelines that parse ls -R can break on filenames containing unusual whitespace, newlines, or other characters. tree is appropriate for human-readable display; find with null-delimited output is a better foundation for scripts that must handle arbitrary filenames safely.

find /path/to/directory -print0

Conclusion

Use tree when you want a readable visualization and find when you need scriptable traversal. Both are clearer and more robust than building a directory hierarchy by parsing recursive ls output.

Related Posts

chevron-up