1.tree是哪些
浏览博客的时侯linux vi 命令,我相信好多人都跟我一样,见到过好多文章里都有很漂亮的目录结构。诸如这样子。
.
└── test
├── css
├── img
│ └── head
└── js
听到这样的目录结果,打心底里感觉舒服——简洁、优雅,在这个世界上只有条纹衫能匹配这样的美了。
然而等到自己写博客想搞目录结构的时侯就好惨了。这是如何生成的呢?真是百思不得其姐。百度也不晓得怎么下手。当初为了在博客上搞个目录结构,我是活生生用鼠标敲起来的。后来也是碰巧的机会才晓得有tree这个工具。
明天就讲解下tree命令。
2.tree的使用方式
tree[OPTIONS][directory]
tree常见的命令如下:
3tree命令的详尽讲解
我有一个test文件夹,文件夹下有如下文件。(以下目录结构为手敲,原本想截图,可惜我的makedown不支持图片)
imga.pngjsindex.js.bablelrchide.txtindex.html说明.txt
我们以这个目录为反例,来说明tree的用法。有几点须要特殊说明。
以下命令都是在test文件夹下打开命令行中标麒麟linux,之后执行命令的。
3.1直接执行tree命令
直接在命令行(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
3.2treea
显示所有的文件和目录,包裹不含文件名的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
3.3tree-A(注意这儿是大A)
使用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码相同,以后的字符全是某个国家语言的所有字符。
3.4tree-C
.
└── test
├── css
│ ├── jquery-ui.css
│ └── main.css
├── hide.txt
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── index.html
├── js
│ └── index.js
└── 说明.txt
补充:倘若未设置LS_COLORS环境变量,则使用外置颜色默认值仍然打开颜色。用于将输出着色到管线。
3.5tree-d
显示目录名称而非内容
tree -d
.
└── test
├── css
├── img
│ └── head
└── js
3.6tree-f
在每位文件或目录之前,显示完整的相对路径名称
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
3.7tree-F
按照ls-F,为目录添加一个'/',为套接字文件添加一个'=',为可执行文件添加一个'*',为FIFO添加一个'|'
.
└── test/
├── css/
│ ├── jquery-ui.css*
│ └── main.css*
├── hide.txt*
├── img/
│ ├── a.png*
│ └── head/
│ └── b.png*
├── index.html*
├── js/
│ └── index.js*
└── 说明.txt*
3.8tree-g
列举文件或目录的所属群组名称(虽然就是文件所属用户组名称),没有对应的名称时,则显示群组辨识码。
.
└── [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
3.9tree-i
不以阶梯状列举文件或目录名称
.
test
css
jquery-ui.css
main.css
hide.txt
img
a.png
head
b.png
index.html
js
index.js
说明.txt
3.10tree-Ipattern
其中pattern为键值,不显示符合的文件或目录名称。
例如我们不显示css文件
tree -I *.css
.
└── test
├── css
├── hide.txt
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── index.html
├── js
│ └── index.js
└── 说明.txt
3.11tree-l
追随目录的符号链接,如同它们是目录一样。防止了造成递归循环的链接。(不晓得这个是干嘛的,测试也没疗效。假如有人晓得请告诉我)
3.12tree-n
不在文件和目录清单加勾线彩。(默认控制台复印下来的目录结构中,文件夹是高亮的,使用-n后,文件夹就不高亮了)
3.13tree-N
按原貌复印不可复印的字符。(看上去也没啥疗效,应当是我的测试案例太正常了)。经过大鳄提醒,假如英文名是英文linux文件目录结构,不加-N有些笔记本上是乱码的。
.
└── test
├── css
│ ├── jquery-ui.css
│ └── main.css
├── hide.txt
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── index.html
├── js
│ └── index.js
└── 说明.txt
3.14tree-p
列举权限标识
.
└── [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
3.15tree-P(小写)pattern
仅列举与键值模式匹配的文件。
例如仅显示css文件
tree -P *.css
.
└── test
├── css
│ ├── jquery-ui.css
│ └── main.css
├── img
│ └── head
└── js
注意:您必须使用-a选项来考虑以点“.”开头的这些文件。有效的键值运算符是*匹配任何零个或多个字符,?匹配任何单个字符,[...]匹配括弧内列举的任何单个字符,[^...]匹配非[]中的任意字符,(|)匹配|前的和后的表达式。
3.16tree-P配合-a使用
这样我们就可以复印下来所有的问题linux文件目录结构,包括隐藏的和没有文件名的文件
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
3.17tree-q
将文件名中的不可复印字符作为问号复印。
由于我的文件名太正常了,所以测试不下来疗效
.
└── test
├── css
│ ├── jquery-ui.css
│ └── main.css
├── hide.txt
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── index.html
├── js
│ └── index.js
└── 说明.txt
3.18tree-s
列举文件或目录大小。
.
└── [ 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
3.19tree-t
根据文件和目录的修改时间排序。注意文件复印的次序不一样了
.
└── test
├── index.html
├── js
│ └── index.js
├── css
│ ├── jquery-ui.css
│ └── main.css
├── img
│ ├── a.png
│ └── head
│ └── b.png
├── 说明.txt
└── hide.txt
3.20tree-u
列举文件或目录的拥有者名称,没有对应的名称时,则显示用户辨识码。
.
└── [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
3.21tree-x
将范围局限在现行的文件系统中,若指定目录下的个别子目录,其储存于另一个文件系统上,则将该子目录给以排除在找寻范围外
由于我的目录太正常了,这个没测试下来。
4安装tree
见到tree如此强悍,是不是很心动?不过tree只在linux和mac下才有这样强悍的疗效。linux和mac下tree工具不是自带的,须要安装。
window是自带tree的,不过实现的简直不能再垃圾了,可以用舌头以下都失明来形容。因此,我开发了一个npm包,来实现tree命令。
可以点击这儿进行安装ctree-cli。假如有问题,欢迎递交issuses