浏览博客的时候,我相信不少人都跟我同样,看到过不少文章里都有很漂亮的目录结构。好比这样子。css
.
└── test
├── css
├── img
│ └── head
└── js
复制代码
看到这样的目录结果,打心底里以为舒服——简洁、优雅,在这个世界上只有格子衫能匹配这样的美了。html
但是等到本身写博客想搞目录结构的时候就捉急了。这是怎么生成的呢?真是百思不得其姐。百度也不知道如何下手。当年为了在博客上搞个目录结构,我是硬生生用键盘敲上去的。后来也是偶然的机会才知道有tree这个工具。jquery
今天就讲解下tree命令。linux
tree [OPTIONS] [directory]shell
tree 常见的命令以下:npm
我有一个test文件夹,文件夹下有以下文件。(如下目录结构为手敲,原本想截图,惋惜个人makedown不支持图片)bash
咱们以这个目录为例子,来讲明tree的用法。有几点须要特殊说明。babel
如下命令都是在test文件夹下打开命令行,而后执行命令的。ide
直接在命令行(terminal)上执行tree命令,工具
tree
复制代码
显示结果以下,显示了全部的文件和目录,除了没有文件名的文件(bebel配置文件):
.
└── test
├── css
│ ├── jquery-ui.css
│ └── main.css
├── hide.txt
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── index.html
├── js
│ └── index.js
└── 说明.txt
复制代码
显示全部的文件和目录,包裹不含文件名的babel文件
tree a
复制代码
.
└── test
├── .babelrc
├── css
│ ├── jquery-ui.css
│ └── main.css
├── hide.txt
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── index.html
├── js
│ └── index.js
└── 说明.txt
复制代码
使用ASNI绘图字符显示树状图而非以ASCII字符组合。不包含没有文件名的文件(这点很奇怪)
.
└── test
├── css
│ ├── jquery-ui.css
│ └── main.css
├── hide.txt
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── index.html
├── js
│ └── index.js
└── 说明.txt
复制代码
补充一点:ANSI编码用0x00~0x7f范围的1个字节来表示1个英文字符,超出一个字节的0x80~0xFFFF范围来表示其余语言的其余字符。也就是说,ANSI码仅在前126个与ASCII码相同,以后的字符全是某个国家语言的全部字符。
.
└── test
├── css
│ ├── jquery-ui.css
│ └── main.css
├── hide.txt
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── index.html
├── js
│ └── index.js
└── 说明.txt
复制代码
补充:若是未设置LS_COLORS环境变量,则使用内置颜色默认值始终打开颜色。用于将输出着色到管道。
显示目录名称而非内容
tree -d
复制代码
.
└── test
├── css
├── img
│ └── head
└── js
复制代码
在每一个文件或目录以前,显示完整的相对路径名称
tree -f
复制代码
.
└── ./test
├── ./test/css
│ ├── ./test/css/jquery-ui.css
│ └── ./test/css/main.css
├── ./test/img
│ ├── ./test/img/a.png
│ └── ./test/img/head
│ └── ./test/img/head/b.png
├── ./test/index.html
└── ./test/js
└── ./test/js/index.js
复制代码
根据ls -F,为目录添加一个' /',为套接字文件添加一个' =',为可执行文件添加一个' *',为FIFO添加一个' |'
.
└── test/
├── css/
│ ├── jquery-ui.css*
│ └── main.css*
├── hide.txt*
├── img/
│ ├── a.png*
│ └── head/
│ └── b.png*
├── index.html*
├── js/
│ └── index.js*
└── 说明.txt*
复制代码
列出文件或目录的所属群组名称(其实就是文件所属用户组名称),没有对应的名称时,则显示群组识别码。
.
└── [root ] test
├── [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
复制代码
不以阶梯状列出文件或目录名称
.
test
css
jquery-ui.css
main.css
hide.txt
img
a.png
head
b.png
index.html
js
index.js
说明.txt
复制代码
其中pattern为通配符,不显示符合的文件或目录名称。
好比咱们不显示css文件
tree -I *.css
复制代码
.
└── test
├── css
├── hide.txt
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── index.html
├── js
│ └── index.js
└── 说明.txt
复制代码
跟随目录的符号连接,就像它们是目录同样。 避免了致使递归循环的连接。(不知道这个是干吗的,测试也没效果。若是有人知道请告诉我)
不在文件和目录清单加上色彩。(默认控制台打印出来的目录结构中,文件夹是高亮的,使用-n后,文件夹就不高亮了)
按原样打印不可打印的字符。(看起来也没啥效果,应该是个人测试案例太正常了)。通过大佬提醒,若是中文名是中文,不加-N有些电脑上是乱码的。
.
└── test
├── css
│ ├── jquery-ui.css
│ └── main.css
├── hide.txt
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── index.html
├── js
│ └── index.js
└── 说明.txt
复制代码
列出权限标示
.
└── [drwxrwxrwx] test
├── [drwxrwxrwx] css
│ ├── [-rwxrwxrwx] jquery-ui.css
│ └── [-rwxrwxrwx] main.css
├── [-rwxrwxrwx] hide.txt
├── [drwxrwxrwx] img
│ ├── [-rwxrwxrwx] a.png
│ └── [drwxrwxrwx] head
│ └── [-rwxrwxrwx] b.png
├── [-rwxrwxrwx] index.html
├── [drwxrwxrwx] js
│ └── [-rwxrwxrwx] index.js
└── [-rwxrwxrwx] 说明.txt
复制代码
仅列出与通配符模式匹配的文件。
好比仅显示css文件
tree -P *.css
复制代码
.
└── test
├── css
│ ├── jquery-ui.css
│ └── main.css
├── img
│ └── head
└── js
复制代码
注意:您必须使用-a选项来考虑以点“.”开头的那些文件。有效的通配符运算符是*匹配任何零个或多个字符,?匹配任何单个字符,[...]匹配括号内列出的任何单个字符,[^...]匹配非[]中的任意字符,(|)匹配|前的和后的表达式。
这样咱们就能够打印出来全部的问题,包括隐藏的和没有文件名的文件
tree -P *.* -a
复制代码
.
└── test
├── .babelrc
├── css
│ ├── jquery-ui.css
│ └── main.css
├── hide.txt
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── index.html
├── js
│ └── index.js
└── 说明.txt
复制代码
将文件名中的不可打印字符做为问号打印。
由于个人文件名太正常了,因此测试不出来效果
.
└── test
├── css
│ ├── jquery-ui.css
│ └── main.css
├── hide.txt
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── index.html
├── js
│ └── index.js
└── 说明.txt
复制代码
列出文件或目录大小。
.
└── [ 4096] test
├── [ 4096] css
│ ├── [ 22175] jquery-ui.css
│ └── [ 2874] main.css
├── [ 0] hide.txt
├── [ 0] img
│ ├── [ 131037] a.png
│ └── [ 0] head
│ └── [ 131037] b.png
├── [ 4725] index.html
├── [ 0] js
│ └── [ 3942] index.js
└── [ 0] 说明.txt
复制代码
按照文件和目录的更改时间排序。注意文件打印的顺序不同了
.
└── test
├── index.html
├── js
│ └── index.js
├── css
│ ├── jquery-ui.css
│ └── main.css
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── 说明.txt
└── hide.txt
复制代码
列出文件或目录的拥有者名称,没有对应的名称时,则显示用户识别码。
.
└── [root ] test
├── [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
复制代码
将范围局限在现行的文件系统中,若指定目录下的某些子目录,其存放于另外一个文件系统上,则将该子目录予以排除在寻找范围外
由于个人目录太正常了,这个没测试出来。
看到tree这么强大,是否是很心动?不过tree只在linux和mac下才有这样强大的效果。linux和mac下tree工具不是自带的,须要安装。
window是自带tree的,不过实现的简直不能再垃圾了,能够用脖子如下都截肢来形容。为此,我开发了一个npm包,来实现tree命令。
能够点击这里进行安装ctree-cli。若是有问题,欢迎提交issuses