tree — Directory Tree Listing
The tree command displays directories and files in a hierarchical, tree-like format.
Unlike ls, which shows a flat list, tree visualizes the full structure of a directory.
This makes it extremely useful for:
- WordPress structure inspection
- Backup validation
- Security audits
- Plugin/theme analysis
- Documentation and reporting
If you need to understand how folders are organized at a glance, tree is the right tool.
1. Installation & Verification
tree is not always installed by default.
Install (Ubuntu/Debian)
sudo apt update && sudo apt install tree -y
Check Installation
which tree
Expected output:
/usr/bin/tree
Check Version
tree --version
Important WordPress Paths
/var/www/html/→ WordPress root/var/www/html/wp-content/plugins//var/www/html/wp-content/themes//var/www/html/wp-content/uploads/
2. Core Syntax
tree [OPTIONS] [DIRECTORY]
If no directory is specified, tree runs in the current directory.
Example:
tree
3. Why Use tree Instead of ls
ls | tree |
|---|---|
| Flat list | Hierarchical layout |
| Good for file inspection | Good for structural inspection |
| Fast | Visual |
| Minimal formatting | Structured branches |
For large WordPress installations, tree helps you understand plugin/theme layout instantly.
4. Core Practical Options (Most Useful First)
Limit Depth (-L N)
tree -L 2 /var/www/html
Shows only two levels deep.
Best practice: always limit depth on large directories.
Directories Only (-d)
tree -d /var/www/html/wp-content
Shows only folders, not files.
Useful for checking:
- Plugin names
- Theme folders
- Upload year/month structure
Show Hidden Files (-a)
tree -a /var/www/html
Reveals:
.htaccess.user.ini.env
Critical for debugging WordPress configuration issues.
Show Full Path (-f)
tree -f /var/www/html
Displays absolute paths.
Useful for migration scripts or automation.
Exclude Pattern (-I)
tree -I "*.log" /var/www/html
Ignores matching patterns.
Helpful for:
- Ignoring logs
- Skipping cache directories
- Reducing noise
Color Output (-C)
tree -C
Colorizes output for readability.
5. File Information Options
Show Permissions (-p)
tree -p /var/www/html
Displays Unix permission bits.
Useful for WordPress security audits.
Show Owner (-u)
tree -u /var/www/html
Detects ownership issues (e.g., root vs www-data).
Show Group (-g)
tree -g /var/www/html
Confirms correct group assignments.
Show File Size (-s)
tree -s
Displays file sizes in bytes.
Human-Readable Sizes (-h)
tree -sh
Shows KB/MB/GB.
Show Directory Cumulative Size (--du)
tree --du -h /var/www/html/wp-content
Displays total size per directory.
Excellent for:
- Finding large upload folders
- Identifying storage-heavy plugins
Show Inodes (--inodes)
tree --inodes
Useful when debugging inode exhaustion.
6. Sorting & Display Control
Sort by Modification Time (-t)
tree -t
Recent files first.
Useful for:
- Detecting recent hacks
- Tracking updated plugins
Reverse Order (-r)
tree -r
Oldest files first.
Natural Version Sorting (-v)
tree -v backups
Sorts numerically:
backup1
backup2
backup10
Directories First (--dirsfirst)
tree --dirsfirst
Improves readability in WordPress root.
Stay on Same Filesystem (-x)
tree -x /
Prevents crossing into mounted drives.
Useful in backup scans.
7. Exporting Output
JSON Output (-J)
tree -J /var/www/html > wp-tree.json
Machine-readable output.
XML Output (-X)
tree -X /var/www/html > wp-tree.xml
Enterprise integrations.
HTML Output (-H)
tree -H . /var/www/html > report.html
Generates browsable HTML report.
Write to File (-o)
tree -L 2 /var/www/html -o wp-structure.txt
Save structure snapshot.
8. WordPress VPS Practical Examples
Inspect WordPress root structure
tree -L 2 /var/www/html
Show plugin directories only
tree -d /var/www/html/wp-content/plugins
Find large uploads folder
tree --du -h /var/www/html/wp-content/uploads
Security audit view
tree -pug /var/www/html
Shows:
- Permissions
- Owner
- Group
Export site structure to file
tree -L 3 /var/www/html -o site-structure.txt
9. Benefits
- Clear visual hierarchy
- Faster structural debugging
- Useful for documentation
- Supports JSON/XML/HTML export
- Ideal for WordPress audits
10. Best Practices
- Always use
-Lon large directories. - Use
-Ito exclude logs and cache. - Combine
-pugshfor audit reports. - Redirect output to file for documentation.
- Avoid running on
/without-L.
11. Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
| -- | - | -- |
| Too many lines | No depth limit | Use -L |
| Hidden files missing | -a not used | Add -a |
| Permission denied | Insufficient rights | Use sudo |
| No colors | Terminal limitation | Use -C |
12. Cheat Sheet
tree -L 2 # Limit depth
tree -d # Directories only
tree -a # Show hidden files
tree -pugsh # Permissions + owner + size
tree -I '*.log' # Exclude pattern
tree --du -h # Show cumulative sizes
tree -J # JSON output
tree -H . > report.html # HTML report
13. Final Perspective
tree is a visualization tool for filesystem hierarchy.
In WordPress VPS environments, it helps you:
- Understand structure quickly
- Audit permissions
- Track large folders
- Document site layout
- Detect anomalies
If ls is inspection, tree is architecture visualization.
Mini Knowledge Check
- What does
tree -dshow? - How do you limit depth to 2 levels?
- Which option shows cumulative directory sizes?
- How do you generate an HTML report?