← 返回命令列表

Linux command

tree 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Display directory tree

tree

Show tree for specific directory

tree [path/to/directory]

Include hidden files

tree -a

Show only directories

tree -d

Limit depth

tree -L [2]

Show file sizes

tree -s

Show human-readable sizes

tree -h

Output in JSON format

tree -J

Exclude patterns

tree -I "[pattern|pattern2]"

Show full path

tree -f

Sort by modification time

tree -t -r

Show directory sizes and respect .gitignore

tree --du --gitignore

说明

tree displays the contents of directories in a tree-like format, showing the hierarchical relationship between files and subdirectories visually. The default output uses ASCII or Unicode box-drawing characters to represent the tree structure. Colorization helps distinguish file types (directories, executables, symlinks, etc.). Various output formats are supported for integration with other tools: JSON (-J) for programmatic parsing, XML (-X) for structured data, and HTML (-H) for web display. Filtering with -I (exclude) and -P (include) uses shell glob patterns, supporting wildcards like \* and ?.

参数

-a
Show all files including hidden
-d
List directories only
-L _level_
Limit depth to level
-f
Print full path prefix
-s
Print size of each file
-h
Print sizes in human-readable format
-D
Print last modification date
-p
Print file permissions
-u
Print username
-g
Print group name
-I _pattern_
Exclude files matching pattern
-P _pattern_
List only files matching pattern
--dirsfirst
List directories before files
-C
Colorize output
-n
Turn off colorization
-r
Sort output in reverse order
-t
Sort by last modification time
-o _filename_
Send output to filename
--du
Show cumulative directory sizes
--prune
Prune empty directories from output
--gitignore
Use .gitignore files for filtering
--noreport
Omit file and directory count at end of listing
--filelimit _N_
Do not descend directories with more than N entries
-H _baseHREF_
Output HTML with base URL

FAQ

What is the tree command used for?

tree displays the contents of directories in a tree-like format, showing the hierarchical relationship between files and subdirectories visually. The default output uses ASCII or Unicode box-drawing characters to represent the tree structure. Colorization helps distinguish file types (directories, executables, symlinks, etc.). Various output formats are supported for integration with other tools: JSON (-J) for programmatic parsing, XML (-X) for structured data, and HTML (-H) for web display. Filtering with -I (exclude) and -P (include) uses shell glob patterns, supporting wildcards like \* and ?.

How do I run a basic tree example?

Run `tree` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -a do in tree?

Show all files including hidden