Linux Command: tree
List directory contents in a tree-like format
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.
Syntax
tree [OPTION]... [DIRECTORY]Key Options
-Lโ Limit recursion depth-dโ List directories only-aโ Show hidden files (starting with .)-hโ Print sizes in human-readable format-Pโ Show only files matching pattern-Iโ Exclude files matching pattern
Examples
View project structure
tree -L 2 src/Output: src/
โโโ controllers/
โ โโโ HomeController.php
โ โโโ BookController.php
โโโ models/
โโโ views/
Directories only
tree -d -L 3 /var/wwwExclude common clutter
tree -I 'node_modules|.git|vendor|__pycache__' .Pro Tips
- Use -I to exclude noise: tree -I 'node_modules|.git|vendor|dist|build' shows only your actual code structure.
- tree -J outputs a JSON representation of the directory structure, useful for programmatic analysis and documentation generation.
- Without -L depth limit, tree will recurse through everything including node_modules (which can have 50,000+ files). Always use -L or -I.
Learn more: Full tree reference โ