Linux Command Reference ยท Files and Directories
Linux tree command
Reference for the Linux tree command: draw directories as a tree, limit depth with -L, filter with -I and -P, show sizes, and a find-based fallback.
tree prints a directory and everything below it as an indented tree, with lines connecting parents to children. It is the fastest way to understand the shape of a project, to paste a layout into documentation or a bug report, or to check what a script just created.
tree is not part of coreutils. Many distributions ship it as a separate package (apt install tree, dnf install tree, brew install tree), and our practice server does not have it. The examples below therefore show real tree output as read-only text and run a small find-based stand-in that you can use anywhere.
tree [OPTION]... [DIRECTORY]...
Try it
This is what the real command prints for a small project. It is read-only: the command is not installed on our runner.
$ tree -F site site/ โโโ css/ โ โโโ main.css โโโ img/ โ โโโ icons/ โ โโโ logo.svg โโโ index.html โโโ js/ โโโ app.js 4 directories, 4 files
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
tree [-adfFhLpsDIP] [-L LEVEL] [-I PATTERN] [-P PATTERN] [DIRECTORY]...
| Option | Description |
|---|---|
-L LEVEL |
Descend at most LEVEL levels. |
-a |
Include hidden entries (names starting with a dot). The default hides them. |
-d |
List directories only. |
-F |
Append / to directories, * to executables, @ to symlinks. |
-f |
Print the full path of each entry. |
-I PATTERN |
Exclude entries matching the shell pattern; separate several with | (-I 'node_modules|dist'). |
-P PATTERN |
Show only files matching the pattern (directories are still walked). |
-s / -h |
Show the size in bytes / in human-readable form. |
-p |
Show the permission string. |
-D |
Show the last modification time. |
--dirsfirst |
List directories before files. |
--gitignore |
Skip what .gitignore excludes (newer versions). |
-J / -X |
Output as JSON / XML (newer versions). |
-C / -n |
Force colour on / off. |
Returns: 0 on success; non-zero if a directory could not be read, with a message for it.
Usage notes
- Hidden files are skipped unless you pass
-a.-aalso walks.git, which can print thousands of lines; combine with-I .git. - Always limit depth on big trees.
tree -L 2in a home directory is useful;treeon/is not. -Iand-Puse shell-style patterns, not regular expressions, and apply to names, not paths.-Pneeds--pruneto also hide directories that end up empty.- Summary line: the final "N directories, M files" counts what was printed, so it changes when you filter.
- Sorting follows the locale. For stable results in scripts use
LC_ALL=C tree ...or--noreportto drop the summary. - Symlinks are shown as
name -> targetand not followed unless you pass-l; following can loop forever on cyclic links. - No
tree? Thefind-based approach above gives a flat listing with-maxdepth,-typeand-not -pathfilters, andls -Rgives a recursive listing. - Copy-friendly output: use
tree --charset asciiwhen the box-drawing characters would be mangled in an email or terminal.
Related
ls,find,mkdir,stat- Lessons: Navigation, Finding files