🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

tree Command

Beginner File Viewing & Searching man(1)

List directory contents in a tree-like format

👁 8 views 📅 Updated: Mar 15, 2026
SYNTAX
tree [OPTION]... [DIRECTORY]

What Does tree Do?

The tree command displays directory structures as an indented tree diagram, showing files and subdirectories in a visually organized hierarchy. It provides a much clearer picture of directory organization than ls.

tree recursively lists all files and directories, showing the parent-child relationships with ASCII art connecting lines. It supports filtering by pattern, limiting depth, showing sizes, and outputting in various formats including HTML and JSON.

This command is invaluable for documenting project structures, understanding unfamiliar codebases, creating directory listings for documentation, and auditing filesystem organization.

Options & Flags

OptionDescriptionExample
-L Limit recursion depth tree -L 2 /var/www
-d List directories only tree -d src/
-a Show hidden files (starting with .) tree -a ~/
-h Print sizes in human-readable format tree -h --du /var/log
-P Show only files matching pattern tree -P '*.php' src/
-I Exclude files matching pattern tree -I 'node_modules|.git' .
--dirsfirst List directories before files tree --dirsfirst src/
-J Output as JSON tree -J src/

Practical Examples

#1 View project structure

Shows the first 2 levels of the src directory.
$ tree -L 2 src/
Output: src/ ├── controllers/ │ ├── HomeController.php │ └── BookController.php ├── models/ └── views/

#2 Directories only

Shows only the directory structure without files.
$ tree -d -L 3 /var/www

#3 Exclude common clutter

Shows the project tree excluding common large directories.
$ tree -I 'node_modules|.git|vendor|__pycache__' .

#4 Show sizes

Shows file sizes and directory totals in human-readable format.
$ tree -h --du /var/log

#5 Filter by file type

Shows only Python files, pruning empty directories.
$ tree -P '*.py' --prune src/

#6 Generate HTML tree

Creates an HTML file with the directory tree — useful for documentation.
$ tree -H . -o structure.html

Tips & Best Practices

Exclude patterns for clean output: Use -I to exclude noise: tree -I 'node_modules|.git|vendor|dist|build' shows only your actual code structure.
JSON output for tooling: tree -J outputs a JSON representation of the directory structure, useful for programmatic analysis and documentation generation.
Can be slow on large directories: Without -L depth limit, tree will recurse through everything including node_modules (which can have 50,000+ files). Always use -L or -I.

Frequently Asked Questions

How do I show only 2 levels of directories?
Use tree -L 2 /path. The -L flag limits recursion depth. Add -d to show only directories without files.
How do I exclude node_modules from tree output?
Use tree -I 'node_modules' to exclude it. For multiple exclusions: tree -I 'node_modules|.git|vendor'.
How do I install tree?
tree is not always pre-installed. Install with: apt install tree (Debian/Ubuntu), yum install tree (RHEL/CentOS), or brew install tree (macOS).

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →