Sagar.BlogArticle
All posts
All posts
Linux

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.

January 9, 20255 min read
LinuxFilesystemTools

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: tree may need to be installed: sudo apt install tree (Debian/Ubuntu) or brew 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 files

Options

tree Options

OptionDescription
-L nLimit depth to n levels (essential for large trees)
-dList directories only (skip files)
-aInclude hidden files and directories
-hHuman-readable file sizes
-sPrint file sizes in bytes
-pPrint permissions
-uPrint owner username
-fPrint full path for each entry
--dirsfirstList directories before files at each level
-I patternExclude items matching pattern
-P patternInclude only items matching pattern
--pruneRemove empty directories from output
-CForce colorized output
-JOutput 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 home

Filtering — 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 dirs

File 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 2

Real-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 paths

tree 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.


Quick Check

You run `tree` on a Node.js project and get thousands of lines from `node_modules`. What's the best fix?

Exercise
  1. Install tree if needed: sudo apt install tree (Ubuntu) or brew install tree (macOS)
  2. Run tree -L 2 ~ — get a 2-level overview of your home directory
  3. Run tree -d -L 3 /etc — see only the directory structure of /etc
  4. In a project directory, try: tree -I "node_modules|.git|dist" -L 3
  5. Save the output of your project tree to a file called structure.txt