How to Use Tree Command in Linux: Syntax and Examples
Use the
tree command in Linux to display a directory and its contents in a tree-like format. Run tree [options] [directory] to see nested folders and files visually. It helps quickly understand folder structures from the terminal.Syntax
The basic syntax of the tree command is:
tree [options] [directory]
Here:
optionsmodify the output (like showing hidden files or limiting depth)directoryis the path you want to display (defaults to current directory if omitted)
bash
tree [options] [directory]
Example
This example shows how to use tree to display the current directory structure with all files and folders:
bash
tree
Output
.
├── file1.txt
├── folder1
│ ├── file2.txt
│ └── subfolder1
│ └── file3.txt
└── folder2
└── file4.txt
3 directories, 4 files
Common Pitfalls
Common mistakes when using tree include:
- Not installing
treefirst (it may not be pre-installed on all Linux systems). - Running
treein very large directories without limiting depth, which can flood the terminal. - Expecting color output without
--coloroption or proper terminal support.
To avoid flooding, use tree -L 2 to limit the display to 2 levels deep.
bash
tree -L 1 # Limits the tree depth to 1 level
Output
.
├── file1.txt
├── folder1
└── folder2
2 directories, 3 files
Quick Reference
| Option | Description |
|---|---|
| -a | Show all files including hidden ones |
| -L | Limit the display to |
| -d | List directories only |
| -f | Print the full path prefix for each file |
| --color | Enable colored output if supported |
| -h | Print the size of each file in a human-readable format |
Key Takeaways
The tree command visually displays directory structures in a nested format.
Use options like -L to limit depth and -a to show hidden files.
Install tree if not available using your package manager (e.g., sudo apt install tree).
Avoid running tree on very large directories without limits to prevent clutter.
Use tree --color for easier reading if your terminal supports colors.