๐ŸŽ New User? Get 20% off your first purchase with code NEWUSER20 ยท โšก Instant download ยท ๐Ÿ”’ Secure checkout Register Now โ†’
Menu

Categories

๐Ÿ’ก File Viewing & Searching September 19, 2026 6

Linux Command: tree

List directory contents in a tree-like format

Terminal โ€” File Viewing & Searching
Command
$ tree -L 2 src/
Output
src/ โ”œโ”€โ”€ controllers/ โ”‚ โ”œโ”€โ”€ HomeController.php โ”‚ โ””โ”€โ”€ BookController.php โ”œโ”€โ”€ models/ โ””โ”€โ”€ views/

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/www

Exclude 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 โ†’

Share this tip