tree — Visualize Directory Structure at a Glance
The tree command prints directory hierarchies as ASCII art. Learn depth limits, filtering, file info, and how to use it to understand any project.
ls shows you what's in one directory. tree shows you the whole picture — files and subdirectories displayed as a connected tree, making it instantly clear how a project or system directory is organized.
Note:
treemay need to be installed:sudo apt install tree(Debian/Ubuntu) orbrew install tree(macOS)
What tree Output Looks Like
tree my-project/
# Output:
my-project/
├── README.md
├── package.json
├── src/
│ ├── index.ts
│ ├── components/
│ │ ├── Header.tsx
│ │ └── Footer.tsx
│ └── utils/
│ └── helpers.ts
├── public/
│ └── index.html
└── tests/
└── app.test.ts
5 directories, 7 filesOptions
tree Options
| Option | Description |
|---|---|
-L n | Limit depth to n levels (essential for large trees) |
-d | List directories only (skip files) |
-a | Include hidden files and directories |
-h | Human-readable file sizes |
-s | Print file sizes in bytes |
-p | Print permissions |
-u | Print owner username |
-f | Print full path for each entry |
--dirsfirst | List directories before files at each level |
-I pattern | Exclude items matching pattern |
-P pattern | Include only items matching pattern |
--prune | Remove empty directories from output |
-C | Force colorized output |
-J | Output in JSON format |
Limiting Depth
Running tree on a large directory like /, /usr, or a node_modules folder would generate millions of lines. Always use -L to limit depth.
tree -L 1 # Only the top level
tree -L 2 # Two levels deep
tree -L 3 /etc # Three levels of /etc
tree -d -L 2 ~ # Only directories, 2 levels in homeFiltering — Include and Exclude
# ─── Exclude noise ───────────────────────────────────────────
tree -I "node_modules" # Skip node_modules
tree -I "node_modules|.git|dist|.next" # Skip multiple (| separated)
tree -I "*.log" # Skip all .log files
# ─── Include only specific files ─────────────────────────────
tree -P "*.ts" # Only TypeScript files
tree -P "*.md" # Only markdown files
tree -P "*.md" --prune # Only markdown, remove empty dirsFile Information
tree -h # Human-readable sizes alongside filenames
tree -s # Exact byte sizes
tree -p # Permissions for each entry
tree -ug # User and group
tree -pugh # All: permissions, user, group, human sizes
# Directories first at every level
tree --dirsfirst -L 2Real-World Combinations
# ─── Project overview (ignoring build artifacts) ─────────────
tree -L 2 -I "node_modules|dist|.git|.next|__pycache__"
# ─── Directory structure only ─────────────────────────────────
tree -d -L 3
# ─── Save structure to file (for docs or sharing) ─────────────
tree -L 3 > project-structure.txt
tree -L 3 -I "node_modules" >> README.md
# ─── JSON output for scripts ─────────────────────────────────
tree -J -L 2 | python3 -m json.tool
# ─── Find all files of an extension ──────────────────────────
tree -P "*.py" --prune -f # All Python files with full pathstree vs find vs ls -R
Use tree when you want a visual overview of structure — great for docs and understanding new projects. Use find when you need to act on files (find + execute, copy, delete). Use ls -R as a quick alternative when tree isn't installed.
You run `tree` on a Node.js project and get thousands of lines from `node_modules`. What's the best fix?
- Install tree if needed:
sudo apt install tree(Ubuntu) orbrew install tree(macOS) - Run
tree -L 2 ~— get a 2-level overview of your home directory - Run
tree -d -L 3 /etc— see only the directory structure of /etc - In a project directory, try:
tree -I "node_modules|.git|dist" -L 3 - Save the output of your project tree to a file called
structure.txt