Linux常用命令-tree

命令

tree

描述

list contents of directories in a tree-like format
显示目录树

用法

1
2
3
4
tree [-acdfghilnpqrstuvxACDFQNSUX] [-H baseHREF] [-T title ] [-L level [-R]]
[-P pattern] [-I pattern] [-o filename] [--version] [--help] [--inodes]
[--device] [--noreport] [--nolinks] [--dirsfirst] [--charset charset]
[--filelimit[=]#] [--si] [--timefmt[=]<f>] [<directory list>]

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Options:
------- Listing options -------
-a 显示所有文件
-d 只显示目录
-l 将目录的软链接也看作目录
-f 显示文件的路径前缀
-x 只显示当前文件系统目录
-L level 指定目录层级
-R 达到最大目录级别时重新运行树
-P pattern 只显示匹配的文件
-I pattern 不显示匹配的文件
--noreport 不显示末尾的统计信息
--charset X 将charset X用于terminal/HTML和缩进行输出
--filelimit # 不要下载超过#个文件的目录
--timefmt <f> 指定时间格式
-o filename 保存到文件
--du 显示目录大小
--prune 不显示空目录
-------- File options ---------
-q 将不可打印的字符打印为?
-N 按原样显示不可打印字符
-Q 文件名用双引号括起来
-p 显示文件权限
-u 显示文件uid
-g 显示文件gid
-s 显示文件大小
-h 显示文件大小的可读形式,单位进制为1024
--si 等同于-h,但是单位进制为1000
-D 显示文件的mtime
-F 添加文件标识符号,同ls -F,如目录后加/
--inodes 显示inode值
--device 显示文件所属的设备ID号
------- Sorting options -------
-v 按版本对文件进行字母数字排序
-r 按字母数字倒序排列
-t 按mtime排序
-c 按ctime排序
-U 不排序
--dirsfirst 目录优先
------- Graphics options ------
-i 不显示左侧目录结构线
-A 使用ANSI字符显示目录结构线
-S 使用ASCII字符显示目录结构线
-n 不显示颜色
-C 显示颜色
------- XML/HTML options -------
-X 显示为XML文件格式
-H baseHREF 显示以baseHREF作为顶部目录的HTML格式
-T string 用字符串替换默认的HTML标题和H1标题
--nolinks 关闭HTML显示中的超链接

注意

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
$ ll
total 4
-rw-r--r-- 1 root root 0 Feb 16 16:49 123
-rw-r--r-- 1 root root 3 Feb 16 16:49 a
-rw-r--r-- 1 root root 0 Feb 16 16:49 a1
lrwxrwxrwx 1 root root 2 Feb 16 16:49 a2 -> a1
-rw-r--r-- 1 root root 0 Feb 16 16:49 .aa
-rw-r--r-- 1 root root 0 Feb 16 16:49 b
-rw-r--r-- 1 root root 0 Feb 16 16:49 c
drwxr-xr-x 2 root root 17 Feb 16 16:49 dir1/
drwxr-xr-x 2 root root 26 Feb 16 16:49 dir2/
lrwxrwxrwx 1 root root 4 Feb 16 16:49 dir3 -> dir1/
$ tree
.
├── 123
├── a
├── a1
├── a2 -> a1
├── b
├── c
├── dir1
│ └── aaa
├── dir2
│ └── bb
└── dir3 -> dir1
3 directories, 8 files
# -a显示所有文件,包括隐藏文件
$ tree -a
.
├── 123
├── a
├── a1
├── a2 -> a1
├── .aa
├── b
├── c
├── dir1
│ └── aaa
├── dir2
│ ├── bb
│ └── .c
└── dir3 -> dir1
# -d只显示目录
$ tree -d
.
├── dir1
├── dir2
└── dir3 -> dir1
# -f显示文件的路径前缀,默认为相对路径
$ tree -f
.
├── ./123
├── ./a
├── ./a1
├── ./a2 -> a1
├── ./b
├── ./c
├── ./dir1
│ └── ./dir1/aaa
├── ./dir2
│ └── ./dir2/bb
└── ./dir3 -> dir1
# 指定绝对路径则显示绝对路径前缀
$ tree -f /tmp
/tmp
├── /tmp/systemd-private-0f406dbb61a34-chronyd.service-eWIbQG
│ └── /tmp/systemd-private-0f406dbb6-chronyd.service-eWIbQG/tmp
├── /tmp/vmware-root_650-2696943027
├── /tmp/vmware-root_651-4013395565
├── /tmp/vmware-root_655-4021587944
├── /tmp/vmware-root_659-4013788787
└── /tmp/vmware-root_661-4013919860
# -L指定目录层级,仅搜索当前层级下的文件
$ tree -L 1
.
├── 123
├── a
├── a1
├── a2 -> a1
├── b
├── c
├── dir1
├── dir2
└── dir3 -> dir1
# -I 排除指定目录
$ tree -I dir1
.
├── 123
├── a
├── a1
├── a2 -> a1
├── b
├── c
├── dir2
│ └── bb
└── dir3 -> dir1
# 排除多个目录,使用|分隔目录
$ tree -I 'dir1|dir2'
# 获取目录结构列表,并排除以下目录
tree --noreport -I 'temp|.trash|bak' -fid /data
# -Q文件名用双引号标识
$ tree -Q
.
├── "123"
├── "a"
├── "a1"
├── "a2" -> "a1"
├── "b"
├── "c"
├── "dir1"
│ └── "aaa"
├── "dir2"
│ └── "bb"
└── "dir3" -> "dir1"
# 显示文件属性信息
$ tree -pugs
.
├── [-rw-r--r-- root root 0] 123
├── [-rw-r--r-- root root 3] a
├── [-rw-r--r-- root root 0] a1
├── [lrwxrwxrwx root root 2] a2 -> a1
├── [-rw-r--r-- root root 0] b
├── [-rw-r--r-- root root 0] c
├── [drwxr-xr-x root root 17] dir1
│ └── [-rw-r--r-- root root 4] aaa
├── [drwxr-xr-x root root 26] dir2
│ └── [-rw-r--r-- root root 3] bb
└── [lrwxrwxrwx root root 4] dir3 -> dir1
# 显示文件属性信息,-i不显示目录结构线
$ tree -pughif
.
[-rw-r--r-- root root 0] ./123
[-rw-r--r-- root root 3] ./a
[-rw-r--r-- root root 0] ./a1
[lrwxrwxrwx root root 2] ./a2 -> a1
[-rw-r--r-- root root 0] ./b
[-rw-r--r-- root root 0] ./c
[drwxr-xr-x root root 17] ./dir1
[-rw-r--r-- root root 4] ./dir1/aaa
[drwxr-xr-x root root 26] ./dir2
[-rw-r--r-- root root 3] ./dir2/bb
[lrwxrwxrwx root root 4] ./dir3 -> dir1
# 只显示文件路径,不显示左侧结构线
$ tree -aif
.
./123
./a
./a1
./a2 -> a1
./b
./c
./dir1
./dir1/aaa
./dir2
./dir2/bb
./dir3 -> dir1
$ tree -aif /data/
/data
/data/db.sqlite3
/data/.env
/data/rsa_key.pem
/data/rsa_key.pub.pem
# 只显示文件名,注意会递归包括子目录下的文件
$ tree -ai
.
123
a
a1
a2 -> a1
b
c
dir1
aaa
dir2
bb
dir3 -> dir1
$ tree -ai /data
/data
db.sqlite3
.env
rsa_key.pem
rsa_key.pub.pem
# 优先显示目录
$ tree --dirsfirst
.
├── dir1
│ └── aaa
├── dir2
│ └── bb
├── dir3 -> dir1
├── 123
├── a
├── a1
├── a2 -> a1
├── b
└── c
# 显示颜色
$ tree -C
.
├── 123
├── a
├── a1
├── a2 -> a1
├── b
├── c
├── dir1
│ └── aaa
├── dir2
│ └── bb
└── dir3 -> dir1
# 生成XML文件
$ tree -X
<?xml version="1.0" encoding="UTF-8"?>
<tree>
<directory name=".">
<file name="123"></file>
<file name="a"></file>
<file name="a1"></file>
<link name="a2" target="a1"></link>
<file name="b"></file>
<file name="c"></file>
<directory name="dir1">
<file name="aaa"></file>
</directory>
<directory name="dir2">
<file name="bb"></file>
</directory>
<link name="dir3" target="dir1"></link>
</directory>
<report>
<directories>3</directories>
<files>8</files>
</report>
</tree>