tree 命令
# tree 命令
# 📖. 前言
浏览博客的时候,我相信很多人都跟我一样,看到过很多文章里都有很漂亮的目录结构。比如这样子。
├── build
├── config
├── docs
│ └── static
│ ├── css
│ └── js
├── src
│ ├── assets
│ ├── components
│ ├── store
│ │ └── modules
│ └── views
│ ├── book
│ └── movie
└── static
2
3
4
5
6
7
8
9
10
11
12
13
14
15
看到这样的目录结果,打心底里觉得舒服——简洁、优雅,在这个世界上只有格子衫能匹配这样的美了。
可是等到自己写博客想搞目录结构的时候就捉急了。这是怎么生成的呢?真是百思不得其姐。百度也不知道如何下手。当年为了在博客上搞个目录结构,我是硬生生用键盘敲上去的。后来也是偶然的机会才知道有tree这个工具。
# 一. 描述
tree
命令用树状图的形式列出一个目录的文件结构
# 二. 安装
可以点击这里进行安装 ctree-cli (opens new window)。
brew install tree
安装成功后,可以直接在终端使用,--help 查看帮助信息。
tree --help
看到如下功能:
➜ ~ tree --help
usage: tree [-acdfghilnpqrstuvxACDFJQNSUX] [-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>]
[--sort[=]<name>] [--matchdirs] [--ignore-case] [--fromfile] [--]
[<directory list>]
------- Listing options -------
-a All files are listed.
-d List directories only.
-l Follow symbolic links like directories.
-f Print the full path prefix for each file.
-x Stay on current filesystem only.
-L level Descend only level directories deep.
-R Rerun tree when max dir level reached.
-P pattern List only those files that match the pattern given.
-I pattern Do not list files that match the given pattern.
--ignore-case Ignore case when pattern matching.
--matchdirs Include directory names in -P pattern matching.
--noreport Turn off file/directory count at end of tree listing.
--charset X Use charset X for terminal/HTML and indentation line output.
--filelimit # Do not descend dirs with more than # files in them.
--timefmt <f> Print and format time according to the format <f>.
-o filename Output to file instead of stdout.
------- File options -------
-q Print non-printable characters as '?'.
-N Print non-printable characters as is.
-Q Quote filenames with double quotes.
-p Print the protections for each file.
-u Displays file owner or UID number.
-g Displays file group owner or GID number.
-s Print the size in bytes of each file.
-h Print the size in a more human readable way.
--si Like -h, but use in SI units (powers of 1000).
-D Print the date of last modification or (-c) status change.
-F Appends '/', '=', '*', '@', '|' or '>' as per ls -F.
--inodes Print inode number of each file.
--device Print device ID number to which each file belongs.
------- Sorting options -------
-v Sort files alphanumerically by version.
-t Sort files by last modification time.
-c Sort files by last status change time.
-U Leave files unsorted.
-r Reverse the order of the sort.
--dirsfirst List directories before files (-U disables).
--sort X Select sort: name,version,size,mtime,ctime.
------- Graphics options -------
-i Don't print indentation lines.
-A Print ANSI lines graphic indentation lines.
-S Print with CP437 (console) graphics indentation lines.
-n Turn colorization off always (-C overrides).
-C Turn colorization on always.
------- XML/HTML/JSON options -------
-X Prints out an XML representation of the tree.
-J Prints out an JSON representation of the tree.
-H baseHREF Prints out HTML format with baseHREF as top directory.
-T string Replace the default HTML title and H1 header with string.
--nolinks Turn off hyperlinks in HTML output.
------- Input options -------
--fromfile Reads paths from files (.=stdin)
------- Miscellaneous options -------
--version Print version and exit.
--help Print usage and this help message and exit.
-- Options processing terminator.
➜ ~
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
# 三. 语法
tree [OPTIONS] <directory>
# 1. 常见的命令如下:
-a
→ 显示所有文件和目录-A
→ 使用 ANSI 绘图字符显示树状图而非以 ASCII 字符组合-C
→ 在文件和目录清单加上色彩,便于区分各种类型。-d
→ 显示目录名称而非内容。-D
→ 列出文件或目录的更改时间。-f
→ 在每个文件或目录之前,显示完整的相对路径名称。-F
→ 根据ls -F
,为目录添加一个'/'
,为套接字文件添加一个'='
,为可执行文件添加一个' *'
,为 FIFO 添加一个' |'
-g
→ 列出文件或目录的所属群组名称,没有对应的名称时,则显示群组识别码。-i
→ 不以阶梯状列出文件或目录名称。-I
→ 不显示符合范本样式的文件或目录名称。-l
→ 跟随目录的符号链接,就像它们是目录一样。避免了导致递归循环的链接-n
→ 不在文件和目录清单加上色彩。-N
→ 按原样打印不可打印的字符。-p
→ 列出权限标示。-P
→ 只显示符合范本样式的文件或目录名称。-q
→ 将文件名中的不可打印字符作为问号打印。-s
→ 列出文件或目录大小。-t
→ 用文件和目录的更改时间排序。-u
→ 列出文件或目录的拥有者名称,没有对应的名称时,则显示用户识别码。-x
→ 将范围局限在现行的文件系统中,若指定目录下的某些子目录,其存放于另一个文件系统上,则将该子目录予以排除在寻找范围外。
# 四. tree 命令详细讲解
有一个 test 文件夹,文件夹里有如下文件。
我们以这个目录为例子,来说明 tree 的用法。有几点需要特殊说明。
- babel 的配置文件
babelrc
是没有文件名的。 说明.txt
的文件名是中文hide.txt
文件属性是隐藏
以下命令都是在 test 文件夹下打开命令行,然后执行命令的。
# 1. tree
直接在终端(Terminal)上执行 tree 命令。
tree
显示结果如下,显示了所有的文件和目录,除了没有文件名的文件(babel 配置文件)):
.
├── css
│ ├── jquery-ui.css
│ └── main.css
├── hide.txt
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── index.html
├── js
│ └── index.js
└── 说明.txt
2
3
4
5
6
7
8
9
10
11
12
13
# 2. tree -a
显示所有的文件和目录,包裹不含文件名的 babel 文件
tree -a
.
├── .babelrc
├── css
│ ├── .DS_Store
│ ├── jquery-ui.css
│ └── main.css
├── hide.txt
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── index.html
├── js
│ └── index.js
└── 说明.txt
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 3. tree -A(主意这里是大A)
使用 ANSI 绘图字符显示树状图而非以 ASCII 字符组合。不包含没有文件名的文件(这点很奇怪)
tree -A
.
├── css
│ ├── jquery-ui.css
│ └── main.css
├── hide.txt
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── index.html
├── js
│ └── index.js
└── 说明.txt
2
3
4
5
6
7
8
9
10
11
12
13
补充一点:ANSI 编码用 0x00~0x7f
范围的 1 个字节来表示 1 个英文字符,超出一个字节的 0x80~0xFFFF
范围来表示其他语言的其他字符。也就是说,ANSI 码仅在前 126 个与 ASCII 码相同,之后的字符全是某个国家语言的所有字符。
# 4. tree -C
tree -C
.
├── css
│ ├── jquery-ui.css
│ └── main.css
├── hide.txt
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── index.html
├── js
│ └── index.js
└── 说明.txt
2
3
4
5
6
7
8
9
10
11
12
13
补充:如果未设置
LS_COLORS
环境变量,则使用内置颜色默认值始终打开颜色。用于将输出着色到管道。
# 5. tree -d
显示目录名而非内容
tree -d
.
├── css
├── img
│ └── head
└── js
2
3
4
5
# 6. tree -f
在每个文件或目录之前,显示完整的相对路径名称。
tree -f
.
├── ./css
│ ├── ./css/jquery-ui.css
│ └── ./css/main.css
├── ./hide.txt
├── ./img
│ ├── ./img/a.png
│ └── ./img/head
│ └── ./img/head/b.png
├── ./index.html
├── ./js
│ └── ./js/index.js
└── ./说明.txt
2
3
4
5
6
7
8
9
10
11
12
13
# 7. tree -F
根据 ls -F
,为目录添加一个 ' /'
,为套接字文件添加一个 ' ='
,为可执行文件添加一个 ' *'
,为FIFO添加一个 ' |'
。
tree -F
.
├── css/
│ ├── jquery-ui.css
│ └── main.css
├── hide.txt
├── img/
│ ├── a.png
│ └── head/
│ └── b.png
├── index.html
├── js/
│ └── index.js
└── 说明.txt
2
3
4
5
6
7
8
9
10
11
12
13
# 8. tree -g
列出文件或目录的所属群组名称(其实就是文件所属用户组名称),没有对应的名称时,则显示群组识别码。
tree -g
.
├── [staff ] css
│ ├── [staff ] jquery-ui.css
│ └── [staff ] main.css
├── [staff ] hide.txt
├── [staff ] img
│ ├── [staff ] a.png
│ └── [staff ] head
│ └── [staff ] b.png
├── [staff ] index.html
├── [staff ] js
│ └── [staff ] index.js
└── [staff ] 说明.txt
2
3
4
5
6
7
8
9
10
11
12
13
# 9. tree -i
不以阶梯状列出文件或目录名称
tree -i
.
css
jquery-ui.css
main.css
hide.txt
img
a.png
head
b.png
index.html
js
index.js
说明.txt
2
3
4
5
6
7
8
9
10
11
12
13
# 10. tree -I pattern
其中 pattern
为通配符,不显示符合的文件或目录名称。
比如我们不显示 css 文件
tree -I css
.
├── hide.txt
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── index.html
├── js
│ └── index.js
└── 说明.txt
2
3
4
5
6
7
8
9
10
# 11. tree -l
跟随目录的符号链接,就像是它们是目录一样。避免了导致递归循环的链接。(不知道这个是干嘛的,测试也没效果。如果有人知道请告诉我)
# 12. tree -n
不在文件和目录清单加上色彩。(默认控制台打印出来的目录结构中,文件夹是高亮的,使用 -n
后,文件夹就不高亮了)
# 13. tree -N
按原样打印不可打印的字符。如果中文名是中文,不加 -N
有些电脑上是乱码的。
tree -N
.
├── css
│ ├── jquery-ui.css
│ └── main.css
├── hide.txt
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── index.html
├── js
│ └── index.js
└── 说明.txt
2
3
4
5
6
7
8
9
10
11
12
13
# 14. tree -p
列出权限标示
tree -P
.
├── [drwxr-xr-x] css
│ ├── [-rw-r--r--] jquery-ui.css
│ └── [-rw-r--r--] main.css
├── [-rw-r--r--] hide.txt
├── [drwxr-xr-x] img
│ ├── [-rw-r--r--] a.png
│ └── [drwxr-xr-x] head
│ └── [-rw-r--r--] b.png
├── [-rw-r--r--] index.html
├── [drwxr-xr-x] js
│ └── [-rw-r--r--] index.js
└── [-rw-r--r--] 说明.txt
2
3
4
5
6
7
8
9
10
11
12
13
# 15. tree -P(大写) pattern
好像没有起作用......
仅列出与通配符模式匹配的文件。
比如仅显示 css 文件
tree -P *.css
注意:
您必须使用 -a
选项来考虑以点 “.”
开头的那些文件。有效的通配符运算符是 * 匹配任何零个或多个字符,? 匹配任何单个字符,[...] 匹配括号内列出的任何单个字符,[^...] 匹配非 [] 中的任意字符,(|) 匹配 | 前的和后的表达式。
# 16. tree -P 配合 -a 使用
好像没有起作用......
这样子我们就可以打印出来所有的问题,包括隐藏的和没有文件名的文件。
tree -P *.* -a
# 17. tree -q
将文件名中的不可打印字符作为问号打印。
因为我的文件名太正常了,所以测试不出来效果
tree -q
.
├── css
│ ├── jquery-ui.css
│ └── main.css
├── hide.txt
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── index.html
├── js
│ └── index.js
└── 说明.txt
2
3
4
5
6
7
8
9
10
11
12
13
# 18. tree -s
件或目录大小。
tree -s
.
├── [ 160] css
│ ├── [ 0] jquery-ui.css
│ └── [ 0] main.css
├── [ 0] hide.txt
├── [ 128] img
│ ├── [ 0] a.png
│ └── [ 96] head
│ └── [ 0] b.png
├── [ 0] index.html
├── [ 96] js
│ └── [ 0] index.js
└── [ 0] 说明.txt
2
3
4
5
6
7
8
9
10
11
12
13
# 19. tree -t
按照文件和目录的更改时间排序。注意文件打印的顺序不一样了
tree -t
.
├── css
│ ├── main.css
│ └── jquery-ui.css
├── js
│ └── index.js
├── hide.txt
├── 说明.txt
├── index.html
└── img
├── head
│ └── b.png
└── a.png
2
3
4
5
6
7
8
9
10
11
12
13
# 20. tree -u
列出文件或目录的拥有者名称,没有对应的名称时,则显示用户识别码。
tree -u
.
├── [root ] css
│ ├── [root ] jquery-ui.css
│ └── [root ] main.css
├── [root ] hide.txt
├── [root ] img
│ ├── [root ] a.png
│ └── [root ] head
│ └── [root ] b.png
├── [root ] index.html
├── [root ] js
│ └── [root ] index.js
└── [root ] 说明.txt
2
3
4
5
6
7
8
9
10
11
12
13
# 21. tree -x
将范围局限在现行的文件系统中,若指定目录下的某些子目录,其存放于另一个文件系统上,则将该子目录予以排除在寻找范围外
因为我的目录太正常了,这个没测试出来。