tree Command Guide for Drawing Directory Trees
This guide helps you understand and use the tree command, which prints the contents of a directory as an indented, readable tree.
What is tree, and Why Use It?
The tree command recursively lists directories and draws them as a branching structure using box-drawing characters, so you can see a whole project layout at a glance.
Why Learn tree?
- Understand a new codebase: See the shape of an unfamiliar repository in seconds.
- Document structure: Tree output is the standard way to describe a project layout in a README.
- Spot clutter: Flat lists hide nesting; a tree makes it obvious where things live.
- Share output:
treeoutput pastes cleanly into issues, docs, and chats.
treeis not installed by default on minimal systems. Install it withsudo apt install tree(Debian/Ubuntu) orsudo dnf install tree(Fedora/RHEL).
1. Tree the Current Directory
tree
- Recursively lists everything below the current directory.
Example:
tree
.
├── cli-navigation
│ ├── cd.md
│ ├── cp.md
│ └── rm.md
├── introduction.md
└── SUMMARY.md
2 directories, 4 files
2. Tree a Specific Directory
tree /etc/nginx/
- Accepts any absolute or relative path.
tree ~
~expands to your home directory.
3. Limit the Depth
tree -L 2
-L 2stops after two levels of nesting — the single most useful flag for large projects.
tree -L 1 -d
- One level, directories only: a quick view of the top-level layout.
4. Show Only Directories or Only Files
tree -d
-dlists directories only.
tree -du -L 3
- Directories only, three levels deep.
There is no built-in “files only” flag. To list files only, use
find . -maxdepth 1 -type f.
5. Control What Is Displayed
tree -a
- Includes hidden files and directories (those beginning with a dot).
tree -I "node_modules|.git"
-I(ignore) excludes entries matching the pattern. Multiple patterns can be separated by|.
tree -C
- Colourises the output by file type.
tree -f
- Prints the full path prefix on each entry, which is handy when copying paths out.
tree -p
- Shows permissions alongside each name.
tree -s
- Shows each entry’s size in bytes.
tree -h
- Human-readable sizes, used together with
-s.
tree -D
- Prints the last modification date instead of the time.
6. Sort the Output
tree -v
- Natural sort order, so
file2precedesfile10.
tree -t
- Sort by modification time, newest first.
tree -rs
- Reverse sort (oldest or smallest first).
7. Save the Output to a File
tree -L 2 > structure.txt
- Redirects the tree into a text file rather than the screen.
tree -L 2 -o structure.txt
-owrites to a file directly. Note that-osuppresses screen output.
tree --charset ascii > structure.txt
- Uses plain ASCII (
+--,|--) instead of Unicode box characters, which is safer for systems or fonts that mangle them.
8. Follow Symlinks Carefully
tree -l 2
-l(lowercase) descends only one level into symlinked directories.
tree -L 3 -l
- Three levels, but symlinked directories are not followed recursively.
By default
treedoes not follow symlinks, which avoids infinite loops. Add-lor-Fonly when you need to.
9. Count Entries
tree --du -h
-ducomputes disk usage for each directory. Combine with-hfor readable sizes.
tree -J > tree.json
-Joutputs JSON, useful for scripting against the result.
Common Pitfalls
- Not installed by default. On minimal servers, install the package first.
- Huge output on
/or~. Always pair with-L,-d, or-Iwhen the directory is large. - Colour lost when redirected. Add
-Cexplicitly if you redirect to a file and want colour. treeon macOS is available viabrew install treeand behaves slightly differently for-Ipatterns.
Related Commands
| Command | Description |
|---|---|
ls | List directory contents, optionally recursively |
find | Search for files matching criteria |
du | Report disk usage per directory |
cd | Move between directories |
pwd | Print the current directory |
tree Cheatsheet
| Command | Description |
|---|---|
tree | Draw the tree for the current directory |
tree path/ | Draw the tree for a specific directory |
tree -L 2 | Limit depth to two levels |
tree -d | Directories only |
tree -a | Include hidden files |
tree -I "pattern" | Ignore matching entries |
tree -C | Colourise output |
tree -f | Print full path prefixes |
tree -p | Show permissions |
tree -s / tree -sh | Show sizes (human-readable) |
tree -v | Natural sort order |
tree -t | Sort by modification time |
tree -o file.txt | Write output to a file |
tree --charset ascii | Use plain ASCII characters |
tree -l | Descend one level into symlinked directories |