前言
有时候当我们再写 README 的的时候需要对项目的结构进行展示的话,这个时候我们就可以很好的利用 Mac 自带的工具 tree,来帮我们快速的生成。
1. 安装 tree
brew install tree
复制代码
2. 参数介绍
参数选项
-a # 显示所有文件,包括隐藏文件(以 “.” 点开头的文件 )
-d # 只显示目录
-f # 只显示每个文件的全路径
-i # 不显示树枝,常与-f参数配合使用
-L # level 遍历目录的最大层数,level 为大于0的正整数
-F # 在执行文件、目录、Socket符号链接、管道名称等不同类型文件的结尾,各自加上“*”、 "/"、"="、"@"、"|"号、类似ls命令的-F选项
复制代码
3. demo 目录
# 测试项目的文件层级关系
.
└── src
└── components
└── common
├── FootCell
│ └── index.vue
├── Pagination
│ └── index.vue
├── Table
│ └── index.vue
└── TitleCell
└── index.vue
复制代码
4. 生成指定文件
进入到要生成 tree 目录:
tree [-d] -L ${number} > ${文件名[.后缀]}
-
tree -L 3 > test1.md
. └── src └── components └── common 3 directories 复制代码 -
tree -d -L 3 > test2.md
. ├── src │ └── components │ └── common 3 directories, 3 files 复制代码
5. 不带任何参数,直接调用 tree
tree # 会在终端直接输出上述demo结果
复制代码
6. 以树形结构显示目录下的所有内容(-a 的功能)
.
├── .DS_Store
└── src
├── .DS_Store
└── components
├── .DS_Store
└── common
├── .DS_Store
├── FootCell
│ └── index.vue
├── Pagination
│ └── index.vue
├── Table
│ └── index.vue
└── TitleCell
└── index.vue
7 directories, 8 files
复制代码
7. 只列出目录下第一层目录的结构(-L 功能)
-
一层
tree -L 1. └── src 复制代码 -
二层
tree -L 2. └── src └── components 复制代码 -
三层
tree -L 3. └── src └── components └── common 复制代码
8. 显示所有目录(但不显示文件)
-
不带路径
tree -d显示
当前文件的目录KaKa:test hhdd$ tree -d # 结果 . └── src └── components └── common ├── FootCell ├── Pagination ├── Table └── TitleCell 7 directories 复制代码 -
带路径
tree -d ${路径}显示
指定路径下的文件的目录KaKa-3:test hhdd$ tree -d /Users/hhdd/Desktop/test # 输出结果 /Users/hhdd/Desktop/test └── src └── components └── common ├── FootCell ├── Pagination ├── Table └── TitleCell 7 directories 复制代码 -
带参数
tree -dL ${number}||tree -d -L ${number}
-d 参数只显示目录,-L 参数显示层数KaKa-3:test hhdd$ tree -dL 1 # 结果 . └── src 1 directory 复制代码
9. -f选项和-i选项的使用
使用
-f选项可显示完整的路径名称,使用-i选项则不显示树枝部分,示例代码如下:
-
-f可显示完整的路径名称KaKa-3:test hhdd$ tree -d -L 2 -f # 结果 . └── ./src └── ./src/components 2 directories 复制代码 -
-i不显示树枝部分KaKa-3:test hhdd$ tree -d -L 2 -f -i # 输出结果 . ./src ./src/components 2 directories 复制代码
10. 使用 tree 命令 区分 目录和文件的方法(常用)
使用
-F参数会在目录后面添加 “/ ”,方便区分目录
-
形式
tree -L ${number} -F [${路径}] -
有路径
KaKa-3:test hhdd$ tree -L 1 -F /Users # 输出结果 /Users ├── Guest/ ├── Shared/ └── hhdd/ 3 directories, 0 files 复制代码 -
无路径参数
KaKa-3:test hhdd$ tree -L 1 -F # 输出结果 . └── src/ 1 directory, 0 files 复制代码 -
对比不加
-FKaKa-3:test hhdd$ tree -L 1 # 输出结果 . └── src 1 directory, 0 files 复制代码
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END





















![[桜井宁宁]COS和泉纱雾超可爱写真福利集-一一网](https://www.proyy.com/skycj/data/images/2020-12-13/4d3cf227a85d7e79f5d6b4efb6bde3e8.jpg)

![[桜井宁宁] 爆乳奶牛少女cos写真-一一网](https://www.proyy.com/skycj/data/images/2020-12-13/d40483e126fcf567894e89c65eaca655.jpg)