In the Ubuntu system, if you want to visually view the files and subfolders structure under a directory, the tree command is your go-to tool. It displays the directory hierarchy in a tree-like diagram, making complex file structures一目了然 (clear at a glance), which is extremely suitable for beginners to quickly understand the directory organization of a project or system.
Step 1: Install the tree command¶
Ubuntu may not have tree pre-installed by default. First, you need to install it. Open the terminal (shortcut Ctrl+Alt+T), and execute the following commands:
sudo apt update # Update the package list (optional, but recommended)
sudo apt install tree # Install the tree command
After entering your password, wait for the installation to complete.
Step 2: Basic Usage¶
Once installed, simply type tree in the terminal and press Enter to see the tree structure of the current directory.
Example: Suppose the current directory has the following structure:
my_project/
├── src/
│ ├── main.py
│ └── utils.py
├── docs/
│ └── guide.md
└── README.txt
After executing tree, the output will be:
.
├── docs
│ └── guide.md
├── README.txt
└── src
├── main.py
└── utils.py
Explanation:
- . represents the current directory, the root node;
- Indentation indicates hierarchical relationships: ├── represents a subdirectory, │ is a separator line, and └── is the last subdirectory;
- All files and subdirectories will be listed.
Step 3: Common Parameters¶
The tree command supports many parameters to flexibly control the displayed content. Here are the most commonly used parameters for beginners, along with examples:
1. Show only directories (-d parameter)¶
If you only want to view folders and not files, use the -d parameter:
tree -d
Example:
.
├── docs
└── src
2. Specify display depth (-L parameter)¶
-L N (where N is a number): Only display the first N levels of directories, avoiding excessively long output when the hierarchy is too deep.
Examples:
- tree -L 1: Show first-level subdirectories of the current directory (excluding subdirectories of subdirectories);
- tree -L 2: Show the first two levels of directories (e.g., current directory → subdirectory → subdirectory of subdirectory).
3. Show full path (-f parameter)¶
-f: Display the full path before each item, which helps in locating the file:
tree -f
Example:
/home/user/my_project
├── /home/user/my_project/docs
│ └── /home/user/my_project/docs/guide.md
├── /home/user/my_project/README.txt
└── /home/user/my_project/src
├── /home/user/my_project/src/main.py
└── /home/user/my_project/src/utils.py
4. Distinguish file types (-F parameter)¶
-F: Add / after directory names, * after executable files, and @ after compressed files to intuitively distinguish types:
tree -F
Example:
.
├── docs/
│ └── guide.md
├── README.txt
└── src/
├── main.py*
└── utils.py
5. Show hidden files (-a parameter)¶
-a: Show hidden files/directories starting with . (common in Linux systems, such as .git, .bashrc, etc.):
tree -a
If there are hidden files in the directory, it will display something like:
.
├── .git/
│ └── config
├── docs/
└── README.txt
6. Show file size (-h parameter)¶
-h: Display file sizes in human-readable units (e.g., K, M, G) to avoid large numbers that are hard to understand:
tree -h
Example:
.
├── docs/
│ └── guide.md (1.2K)
├── README.txt (500B)
└── src/
├── main.py (200B)
└── utils.py (300B)
Step 4: Advanced Usage¶
1. Output to a file¶
If you need to save the directory structure to a text file (for sharing or archiving), use redirection:
tree > directory_structure.txt
After execution, the tree diagram of the current directory will be saved to directory_structure.txt, which can be opened directly to view.
2. Combine with other commands¶
tree can be combined with the find command to view the directory structure of any path in the system. For example:
find /usr/share | tree -L 1 # Show first-level directories under /usr/share (the result may be long, so it is recommended to add -L to limit the depth)
Common Issues¶
- Issue 1: tree is not installed?
Execute the installation command from Step 1: (sudo apt install tree). - Issue 2: How to combine parameters?
Multiple parameters can be used together, e.g.,tree -L 2 -d -h(display the first two levels of directories, only show directories, and show sizes).
Summary¶
The tree command is a “visualization tool” for managing directory structures in the Ubuntu system. It is simple and efficient, suitable for all scenarios where you need to quickly understand file organization. Through this article, you can easily get started:
- Directly use tree to view the current directory;
- Use -L N to control the depth, -d to only view directories, and -F to distinguish file types;
- Use > to output to a file or combine with find to view system directories.
Now, open the terminal and try different parameters to make your file management clearer!