Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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: tree output pastes cleanly into issues, docs, and chats.

tree is not installed by default on minimal systems. Install it with sudo apt install tree (Debian/Ubuntu) or sudo 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 2 stops 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
  • -d lists 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 file2 precedes file10.
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
  • -o writes to a file directly. Note that -o suppresses 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.

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 tree does not follow symlinks, which avoids infinite loops. Add -l or -F only when you need to.


9. Count Entries

tree --du -h
  • -du computes disk usage for each directory. Combine with -h for readable sizes.
tree -J > tree.json
  • -J outputs 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 -I when the directory is large.
  • Colour lost when redirected. Add -C explicitly if you redirect to a file and want colour.
  • tree on macOS is available via brew install tree and behaves slightly differently for -I patterns.

CommandDescription
lsList directory contents, optionally recursively
findSearch for files matching criteria
duReport disk usage per directory
cdMove between directories
pwdPrint the current directory

tree Cheatsheet

CommandDescription
treeDraw the tree for the current directory
tree path/Draw the tree for a specific directory
tree -L 2Limit depth to two levels
tree -dDirectories only
tree -aInclude hidden files
tree -I "pattern"Ignore matching entries
tree -CColourise output
tree -fPrint full path prefixes
tree -pShow permissions
tree -s / tree -shShow sizes (human-readable)
tree -vNatural sort order
tree -tSort by modification time
tree -o file.txtWrite output to a file
tree --charset asciiUse plain ASCII characters
tree -lDescend one level into symlinked directories