tree
Overview
The tree
command displays directory structure in a tree-like format. It’s useful for visualizing directory hierarchies and file organization.
Syntax
tree [options] [directory...]
Common Options
Option | Description |
---|---|
-a |
Show all files |
-d |
List directories only |
-f |
Print full path prefix |
-i |
Don’t print indentation lines |
-l |
Follow symbolic links |
-p |
Print protections |
-s |
Print size |
-h |
Print size in human readable format |
-u |
Print user name |
-g |
Print group name |
-L level |
Max display depth |
--prune |
Prune empty directories |
--filelimit n |
Don’t descend dirs with > n files |
--dirsfirst |
List directories first |
Key Use Cases
- Directory visualization
- Project structure analysis
- File system navigation
- Documentation generation
- Directory comparison
Examples with Explanations
Example 1: Basic Usage
tree
Show directory structure
Example 2: Limited Depth
tree -L 2
Show only two levels deep
Example 3: Directory Only
tree -d
Show only directories
Understanding Output
.
├── dir1
│ ├── file1
│ └── file2
└── dir2
└── file3
2 directories, 3 files
Common Usage Patterns
Project overview:
tree -L 2 project/
Show with details:
tree -pugh
Filter output:
tree -P '*.py'
Performance Analysis
- Directory traversal impact
- Memory usage for large trees
- Output formatting overhead
- Pattern matching speed
- Symbolic link handling
Additional Resources
Output Formatting
- Color options
- HTML output
- XML output
- JSON output
- Custom patterns
Best Practices
- Use depth limits for large directories
- Consider file limits
- Filter unnecessary files
- Use appropriate output format
- Handle symbolic links carefully