tree — Visualize Directory Structure
💬 "tree command"
tree shows the directory structure in a hierarchical shape. It is faster to understand the structure than running ls multiple times.
Use it to quickly check the structure of directories where logs accumulate or deployment artifacts.
- Concept: Outputs subdirectories and files in a tree format starting from the specified path.
- Left side: Represents hierarchy with connector lines like
├──,└──. - Last line: Summarizes the number of directories and files.
- Depth: You can limit how many levels to view with the -L option.
Commonly Used Options
| Option | Description |
|---|---|
| -L <n> | Limits depth to n levels |
| -d | Displays directories only |
| -h | Displays file sizes in human-readable units |
| --du | Displays cumulative size by directory |
| -a | Displays hidden files as well |
3 Essential Command Combinations
tree -L 2 /var/www/html— Check the web root structure up to 2 levels onlytree -d -L 2 /opt/app— View only directories to understand the structuretree --du -h /var/log | head -30— Check size by log directory
💬 "Which options should be used together to reduce mistakes?"
Here are situations commonly encountered on a live server, organized in order.
Step 1 · View the Structure at a Glance
Limit the depth to understand the overall layout.
tree -L 2 /var/www/html
Step 2 · View with Sizes
Check which subfolders are large.
tree --du -h -L 2 /var/log
Step 3 · Save as Documentation
Save the structure to a file for review or handover.
tree -L 3 /opt/app > /tmp/app-structure.txt
How This Is Used in Practice
1. Verify Deployment Structure
Situation — You unpacked a release archive, but the file layout is different from expected.
tree -L 2 /opt/app/releases/20261018
Result — If public/ should be at the top level but a subfolder goes one level deeper, it is an archive path issue.
Point — The habit of checking with tree after deployment helps you quickly find the cause of 404/500 errors.
2. Identify Where Log Data Accumulates
Situation — The disk is full, so you check which log directory is large.
tree --du -h -L 2 /var/log | sort -k2 -h | tail
Result — Identify the large subdirectories and decide what to rotate.
Point — Because tree's --du shows recursive totals, it is convenient for judging at the folder level.
Things to Keep in Mind
- If you run tree without options in a directory with tens of thousands of files, the output explodes. Limit it with -L.
- tree is not always installed by default, so if the tree command is missing, you need to install the package.
- If the output is long, trim it with head/tail.
Actual Execution Screen
This is the result of checking the two-level structure of the web root.
!tree command execution result: directory tree structure
A live production server
Source (HTML): https://games.aidebatehub.com/programs/linux/filesystem/tree.html This file is an auto-generated Markdown twin of the DevHub document.
AI Knowledge Hub
Visitor Comments