Overview
The tree command is a tool that displays the hierarchical structure of files and subdirectories in a visual “tree” format.
Unlike the recursive listing of ls -R, which can be hard to follow, tree allows you to understand the entire overview of a folder and the relationships between files at a glance. It is also very useful for creating documentation.
Note: This tool is not installed by default in many environments, so you may need to install it first.
# For Ubuntu/Debian systems
sudo apt install tree
# For RHEL/CentOS systems
sudo dnf install tree
Specifications (Arguments and Options)
Syntax
tree [options] [directory_name]
Main Options
| Option | Description |
-d | Displays only directories, hiding files. |
-P [pattern] | Displays only files that match the specified wildcard pattern. |
-I [pattern] | Excludes files that match the specified wildcard pattern. |
-p | Displays file permissions (protections). |
-u | Displays the name of the file owner (username). |
-g | Displays the name of the file group. |
-s | Displays file sizes in bytes. |
-h | Displays file sizes in human-readable units (K, M, G). |
-F | Appends a symbol (such as /, *, @) to indicate the file type. |
-L [number] | Limits the depth of the directory levels to display. |
-N | Prints non-printable characters as is (prevents garbled Japanese characters). |
Basic Usage
Displaying a Directory Structure
Run the command followed by the directory path. If you do not specify a path, it displays the current directory.
tree my_project
Example Output:
my_project
├── config
│ ├── settings.json
│ └── database.yml
├── data
│ ├── raw
│ │ └── input.csv
│ └── processed
├── scripts
│ └── main.py
└── readme.md
5 directories, 4 files
Practical Commands
1. Searching and Displaying Specific Files with Detailed Information
This command displays permissions, owners, groups, and sizes while filtering for a specific extension (e.g., .conf).
# Show permissions(-p), owner(-u), group(-g), and human-readable size(-h)
# Filter for files ending in .conf (-P)
tree -uphg -P "*.conf" /etc/nginx
Example Output:
/etc/nginx
├── [-rw-r--r-- root root 1.0K] fastcgi.conf
├── [-rw-r--r-- root root 2.4K] mime.types
├── [-rw-r--r-- root root 1.5K] nginx.conf
└── [drwxr-xr-x root root 4.0K] conf.d
└── [-rw-r--r-- root root 500] default.conf
1 directory, 4 files
2. Checking the Top Part of a Huge Directory
If a directory has too many files, the output will scroll past your screen. Use the head command to see only the beginning.
# Display only the first 5 lines of the directory structure
tree /var/lib/music/artist_data/ | head -n 5
3. Displaying Only the Directory Structure (Excluding Files)
Use the -d option when you only want to understand the folder layout without being distracted by individual files.
tree -d /var/log
Customization Points
- Exclusion Settings (
-I "pattern"): Use this to hide directories likenode_modules,.git, or__pycache__.- Example:
tree -I "node_modules|.git"
- Example:
- Level Limits (
-L 2): Best for getting a high-level overview without descending into deep subdirectories. - File Type Symbols (
-F): Adds visual indicators (/for directories,*for executables), making it easier to distinguish between different types of files.
Important Notes
- Garbled Characters: If filenames in non-English characters appear as codes like
\343\201..., always add the-Noption to output the original character codes. - Performance Impact: Running
treeon a root directory or a directory with millions of files can consume significant CPU and memory. Always use-LorCtrl+Cto control the output. - Pattern Quoting: Patterns used with
-Por-Ishould be wrapped in double quotes to ensure the shell doesn’t expand them before thetreecommand processes them.
Advanced Application
Saving the Directory Structure to a Text File
To save the directory layout for documentation or system reports, use the following redirection. Using --charset=ascii ensures compatibility with any text editor.
Bash
# Output the structure to a file using ASCII characters for the tree lines
tree --charset=ascii -o directory_structure.txt my_project
Conclusion
The tree command is an indispensable tool for visualizing complex directory hierarchies instantly.
Design Tip: Use the ASCII charset when sharing outputs in environments where special characters might not render correctly.
Ideal Use Cases: Understanding project layouts, creating file lists for delivery, and exploring unknown server environments.
Key Configuration: Combine -d for a clean folder-only view and -L to manage the output depth.
