Linux Directory Tree: Print Folders With the tree Command
November 11, 2026 · 10 min read
Linux Directory Tree: Print Any Folder Structure With the tree Command
A Linux directory tree is the nested view of folders and files that the tree command draws with connecting lines. Install tree with your package manager, limit depth with -L, skip folders like node_modules with -I, and save plain output to a file you can paste into a README.
What Is a Directory Tree in Linux?
Think of it as the hierarchy of folders below a starting point. On Linux everything hangs off the root directory, /, and every path you type walks down that hierarchy one folder at a time. The tree command prints it as indented text instead of the flat listing you get from ls.
Run it on a small project folder:
tree my-app
my-app
├── LICENSE
├── README.md
├── src
│ ├── index.js
│ └── utils
│ └── format.js
└── tests
└── format.test.js
4 directories, 5 files
Each ├── marks an entry with more siblings below it, └── marks the last entry in a folder, and │ carries the parent's line past its children. The final line is a summary. Since tree 2.1.0 that count includes the starting folder, so older releases report one directory fewer for the same project.
How to Install the tree Command on Linux
Check first with tree --version. If the shell can't find it, install the package, which is called tree on every major distribution:
# Debian, Ubuntu, Linux Mint
sudo apt install tree
# Fedora and other dnf-based systems
sudo dnf install tree
# Arch Linux and derivatives
sudo pacman -S tree
# openSUSE
sudo zypper install tree
Versions vary more than you'd expect. Ubuntu 24.04 packages tree 2.1.1, Debian stable has 2.2.1, and Arch and openSUSE Tumbleweed carry the current upstream release, 2.3.2. The flags below work on any 2.x release unless a section says otherwise. If you're on a Mac, the same options apply once tree is installed there. The Mac tree command guide covers that install, and the Windows tree command guide covers Microsoft's separate built-in tool.
Printing a Directory Tree in Linux: Depth, Folders and Hidden Files
Running tree with no arguments prints everything below the current folder, which gets long fast. These are the options you'll reach for most:
tree -L 1 # first level only
tree -L 2 src # two levels, starting inside src
tree -d # directories only, no files
tree -a # include hidden entries such as .env
tree --dirsfirst # list folders before files
tree -f # prefix each entry with its path
tree -h --du # human-readable sizes, with folder totals
Depth is the one that matters most. Here's the same project limited with tree -L 1 my-app:
my-app
├── LICENSE
├── README.md
├── src
└── tests
3 directories, 2 files
Options combine freely, so tree -d -L 2 gives a quick map of a large codebase without a single file name. By default tree skips names that start with a dot. It never prints the . and .. entries, even with -a.
If a script needs the structure rather than a picture, tree -J prints JSON and tree -X prints XML. Pair either with -L so the file stays a sensible size. On tree 2.3.0 or later, --condense folds chains of single-child folders, such as src/main/java, onto one line. That tidies deep Java package folders considerably.
How Do You Exclude a Directory From tree?
Use -I followed by a wildcard pattern. tree hides every file or folder whose name matches. Separate several patterns with a vertical bar, and wrap the whole pattern in single quotes so the shell doesn't expand the asterisk or read the bar as a pipe:
tree -I node_modules
tree -I 'node_modules|dist|coverage'
tree -a -I '.git|node_modules'
tree --gitignore
tree -P '*.md' --prune
tree --filelimit=100
A few details make these work as you'd expect:
- Hidden folders:
.gitdoesn't appear unless you pass-a, so you only need to exclude it when you also show hidden files. - Repeating the flag: since tree 2.0.0, per the upstream changelog, you can pass
-Imore than once instead of joining patterns with a bar. --gitignore: also added in 2.0.0 (December 2021), it hides whatever your.gitignorefiles exclude. Since 2.3.0, tree also searches parent folders up to the repository root for more.gitignorefiles.-Pwith--prune:-Pkeeps only matching files, but folders still print.--prunedrops the folders that end up empty. Add--matchdirsif you want-Pto test folder names too.--filelimit=100: stops tree from descending into any folder with more than 100 entries.
Inside a git repository there's a tidier route: hand tree the list of tracked files. git ls-files | tree --fromfile . builds the drawing from that list instead of the disk, so ignored and untracked files never show up. The dot tells tree to read the paths from standard input.
One catch from the tree manual: filtering with -I, -P or --filelimit makes the summary count inaccurate. Turn the report off with --noreport when you filter.
Saving a Directory Tree to a File or README
Write the output to a file with -o, or redirect it with >:
tree -L 2 --dirsfirst --noreport -n -I 'node_modules|dist' -o tree.txt
tree -L 2 --charset=ascii --noreport > tree.txt
A redirect with > comes out clean, because tree only adds colour when its standard output is a terminal. -o is different: tree still sees your terminal as standard output. If LS_COLORS is set, colour escape codes can land in the file, so add -n whenever you use -o. The -n flag also overrides the CLICOLOR_FORCE variable.
In a UTF-8 locale tree draws box characters, and it pads the vertical bars with non-breaking spaces. --charset=ascii swaps everything for plain pipes, hyphens and backticks with ordinary spaces:
my-app
|-- LICENSE
|-- README.md
|-- src
| |-- index.js
| `-- utils
| `-- format.js
`-- tests
`-- format.test.js
Here's the routine we follow for a README:
- Run tree from the folder above the project and pass the project's name, so the first line shows the name rather than a dot.
- Add a depth limit, your exclusions and
--noreportto the same command. - Open the output and delete anything readers don't need, like lock files and generated folders.
- Paste what's left into the README inside a fenced code block.
Tag that block text. The fence keeps the monospace alignment, and the tag marks it as plain output rather than code:
### Project structure
```text
my-app
├── src
│ └── index.js
└── README.md
```
Our code block guide covers fence syntax in detail, and the README guide covers where a structure section fits in a README. We prefer tree -L 2 --dirsfirst --noreport for READMEs: two levels show the shape of a project without burying readers in file names.
Try it below. The demo holds a trimmed tree inside a fenced block, and the preview keeps every line aligned:
The preview shows the block as plain monospace text without syntax colouring. In the full editor, the Code Block toolbar button or Ctrl+E (Cmd+E on a Mac) inserts the fence for you.
How to Show a Directory Tree Without Installing tree
On a locked-down server where you can't install packages, the standard tools get you most of the way. ls -R lists every folder's contents recursively, one block per folder. find gives you more control:
ls -R
find . -maxdepth 2 -type d
find . -not -path './node_modules*' | sort | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
-maxdepth 2 stops find two levels below the starting point, and -type d keeps only directories. The third line turns each path into an indented row:
.
|____LICENSE
|____README.md
|____src
| |____index.js
| |____utils
| | |____format.js
|____tests
| |____format.test.js
It's rougher than tree's output, and the sort order depends on your locale, but it needs nothing beyond coreutils, findutils and sed.
Linux Directory Structure: What the Top-Level Folders Mean
Some people searching for a Linux directory tree want the system layout rather than a project folder. Run tree -L 1 / to print it. The Filesystem Hierarchy Standard 3.0 defines what each top-level folder is for:
| Directory | What it holds |
|---|---|
/bin | Essential commands for all users |
/boot | Boot loader files and the kernel |
/dev | Device files |
/etc | System-wide configuration |
/home | Users' home directories |
/lib | Shared libraries and kernel modules |
/media, /mnt | Mount points for removable and temporary filesystems |
/opt | Add-on application packages |
/proc, /sys | Virtual filesystems with kernel and process information |
/root | The root user's home directory |
/run | Runtime data since the last boot |
/sbin | System administration binaries |
/tmp | Temporary files |
/usr | Read-only programs, libraries and documentation |
/var | Logs, caches, spools and other changing data |
On many current distributions /bin, /sbin and /lib are symlinks into /usr. tree shows those as bin -> usr/bin, because it prints a symlink's target after its name.
Drawing a Tree for a Project That Doesn't Exist Yet
tree only describes folders that exist on disk. When you're planning a layout, writing docs for a repo you can't reach, or trimming a big tree by hand, the directory tree generator is quicker. Paste an indented list, an existing tree drawing or a list of paths, and it renders box-drawing, ASCII, a Markdown list or tree-style output. It runs in your browser with nothing to install.
Be realistic about either route, though. Neither knows which files matter to your readers, so you'll usually delete lock files, config noise and build output before the tree goes into a README. For a whole docs folder layout, our Markdown documentation guide shows how to organise the pages themselves.
Linux Directory Tree FAQ
A Linux directory tree takes one command once tree is installed: limit depth with -L, hide noise with -I or --gitignore, and save plain ASCII with --charset=ascii --noreport. Paste the result into a text fence and it keeps its shape on GitHub. Want to check the alignment first? Open the editor, paste it in and watch the preview, or build a planned layout with the tree generator.