Linux tree Command
The tree command is used to display files and directories in a hierarchical tree structure.
It is extremely useful for:
- Understanding folder structures
- Documentation
- Development projects
- Linux administration
- Server management
What is the tree command?
Instead of displaying files like:
ls
The tree command shows:
Projects
├── Website
│ ├── index.php
│ └── style.css
├── Scripts
│ └── backup.sh
└── README.md
Installation
Some Linux distributions do not include tree by default.
Ubuntu/Debian:
sudo apt install tree
Fedora:
sudo dnf install tree
Arch Linux:
sudo pacman -S tree
Syntax
tree [OPTIONS] [DIRECTORY]
Basic Usage
tree
Example:
.
├── Documents
├── Downloads
└── Projects
View Specific Directory
tree /etc
Command Options
| Flag | Description |
|---|---|
-a | Show hidden files |
-d | Show only directories |
-L | Limit depth level |
-f | Show full paths |
-h | Human readable sizes |
-s | Show file sizes |
-I | Ignore patterns |
--dirsfirst | Directories first |
-p | Show permissions |
-u | Show owner |
-g | Show group |
tree -a
Display hidden files.
tree -a
Output:
.
├── .bashrc
├── .ssh
└── Documents
tree -d
Display only directories.
tree -d
Output:
.
├── Documents
├── Downloads
└── Projects
tree -L
Limit depth.
tree -L 2
Example:
Projects
├── Website
└── Scripts
tree -f
Show full paths.
tree -f
Output:
/home/aayan/Documents
/home/aayan/Projects
tree -h
Show human readable sizes.
tree -h
tree -s
Show file sizes.
tree -sh
Output:
[4.0K] Documents
[1.2K] file.txt
tree -I
Ignore directories or files.
Example:
tree -I "node_modules"
Multiple patterns:
tree -I "node_modules|vendor|.git"
tree --dirsfirst
Display directories before files.
tree --dirsfirst
tree -p
Display permissions.
tree -p
tree -u
Display owner.
tree -u
tree -g
Display group.
tree -g
Combining Flags
Example:
tree -aL 3
Example:
tree -ah
Example:
tree -aL 3 --dirsfirst
Real Development Example
Laravel Project:
tree -L 2
Output:
app
bootstrap
config
database
public
resources
routes
storage
PocketMine Server Example
tree -L 2
Output:
server
├── plugins
├── worlds
├── logs
├── resource_packs
└── players
Website Example
tree /var/www/html
Output:
html
├── index.php
├── css
├── js
└── images
Ethical Hacking Example
tree /usr/share/wordlists
Common Mistakes
Command not found
Install:
sudo apt install tree
Huge output
Limit depth:
tree -L 2
Exercises
tree
tree -a
tree -L 3
tree -d
tree -I ".git"
Mini Project
Create:
mkdir -p Website/{css,js,images}
touch Website/index.php
Run:
tree Website
Output:
Website
├── css
├── images
├── index.php
└── js
Command Summary
| Command | Purpose |
|---|---|
tree | Display hierarchy |
tree -a | Show hidden files |
tree -d | Directories only |
tree -L | Limit depth |
tree -I | Ignore patterns |
Next Chapter
➡ Linux realpath Command