Quick take: The tree command shows directory structure as an indented tree. Use tree -L 2 to limit depth, -a to include hidden files, and -d to show directories only. Install it with your package manager if it is missing.
Introduction
The tree command displays the contents of a directory as a visual tree, making folder structure far easier to grasp than nested ls output. It is perfect for documenting a project layout, exploring an unfamiliar codebase, or sharing a directory structure in notes.
Syntax
The basic syntax of the tree command is:
tree [OPTIONS] [DIRECTORY]Common Options and Parameters
The most useful options and parameters for the tree command:
| Option | Description |
|---|---|
| -L N | Limit the display to N levels deep. |
| -a | Show hidden files. |
| -d | List directories only. |
| -f | Print the full path for each entry. |
| -h | Show file sizes in human-readable form. |
| -I PAT | Exclude files matching a pattern. |
| --dirsfirst | List directories before files. |
Practical Examples
Real tree commands you can run today:
# Show the tree from the current directory
tree
# Limit to 2 levels deep
tree -L 2
# Directories only
tree -d
# Include hidden files and sizes
tree -ah
# Exclude node_modules and .git
tree -I 'node_modules|.git'
# Install tree on Ubuntu if missing
sudo apt install treeTips and Best Practices
- Use
-Lto keep big projects readable —tree -L 2shows just the top two levels. tree -I 'node_modules|.git'hides noisy directories when documenting a project.- Pipe to a file (
tree -L 2 > structure.txt) to capture a layout for documentation.
Final Thoughts
tree turns directory structure into a clear, shareable diagram. Reach for -L to control depth, -d for a directories-only overview, and -I to hide clutter. It is the friendliest way to document or explore a folder hierarchy, complementing ls and find.
FAQ: tree Command in Linux
How do I show a directory tree in Linux?+
Run tree in the directory, or tree /path for another location. It prints an indented tree of all files and folders. Install it with sudo apt install tree if the command is missing.
How do I limit how deep tree goes?+
Use -L with a number: tree -L 2 shows only two levels, which keeps large projects readable.
How do I show only directories with tree?+
Use -d: tree -d lists folders without their files, giving a clean structural overview.
How do I exclude folders like node_modules?+
Use -I with a pattern: tree -I 'node_modules|.git' hides those directories from the output.
How do I save a tree to a file?+
Redirect the output: tree -L 2 > structure.txt writes the tree to a file, useful for documentation and README files.
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support