Skip to content
Tutorials Center
My learning

Topics

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.

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

Terminal
$ tree -F site
site/
โ”œโ”€โ”€ css/
โ”‚   โ””โ”€โ”€ main.css
โ”œโ”€โ”€ img/
โ”‚   โ”œโ”€โ”€ icons/
โ”‚   โ””โ”€โ”€ logo.svg
โ”œโ”€โ”€ index.html
โ””โ”€โ”€ js/
    โ””โ”€โ”€ app.js

4 directories, 4 files
Bash A tree built from find (stand-in)

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.

Bash Limit depth and hide noise with find

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

Terminal
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. -a also walks .git, which can print thousands of lines; combine with -I .git.
  • Always limit depth on big trees. tree -L 2 in a home directory is useful; tree on / is not.
  • -I and -P use shell-style patterns, not regular expressions, and apply to names, not paths. -P needs --prune to 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 --noreport to drop the summary.
  • Symlinks are shown as name -> target and not followed unless you pass -l; following can loop forever on cyclic links.
  • No tree? The find-based approach above gives a flat listing with -maxdepth, -type and -not -path filters, and ls -R gives a recursive listing.
  • Copy-friendly output: use tree --charset ascii when the box-drawing characters would be mangled in an email or terminal.