前言
我们经常在写博客或者项目设计文档时需要列出项目的结构树。我们下可以使用tree列出项目结构,如下面这种:
news_watch_notice
├── cmd //main
├── conf
├── dis
├── Dockerfile
├── Makefile
├── pkg
├── qrcode
├── .travis.yml
├── README.md
├── vendor
├── utils
复制代码
使用windows自带的tree
windows提供了一个tree命令可供我们使用,但是不太好用。
TREE [drive:][path] [/F] [/A]
/F 显示每个文件夹中文件的名称。
/A 使用 ASCII 字符,而不使用扩展字符。
复制代码
D:\dev\gopath\src\news_watch_notice>tree /a
文件夹 PATH 列表
卷序列号为 989E-DB7E
D:.
+---.idea
+---cmd
| \---qrcode
+---conf
+---dis
| \---qrcode
+---pkg
| +---mail
| +---reptile
| \---wechat
+---qrcode
+---utils
复制代码
D:\dev\gopath\src\news_watch_notice>tree /f
文件夹 PATH 列表
卷序列号为 989E-DB7E
D:.
│ .gitignore
│ .travis.yml
│ Dockerfile
│ Makefile
│ README.md
│ tree.md
│
├─.idea
│ .name
│ misc.xml
│ modules.xml
│ news_watch_notice.iml
│ vcs.xml
│ workspace.xml
│
├─cmd
│ │ news_watch_notice.go
│ │
│ └─qrcode
│ qrcode.jpg
复制代码
可以看到确实按照我们想要的结构输出了,但是只有两个命令的使用并不能满足我们日常的需要,比如我们想忽略某个文件,想把生成的树状结构输出的一个文件中又该如何操作.
使用基于node的tree-node-cli
要想使用这些命令,我们需要做一些准备
- nodejs安装,可以戳这里(建议使用LTS的稳定版本,另nodejs自带了npm安装包管理器)
安装tree-node-cli
#安装tree-node-cli模块包
npm install -g tree-node-cli
复制代码
使用命令
$ tree --help
Usage: tree [options]
Options:
-V, --version output the version number
-a, --all-files All files, include hidden files, are printed.
--dirs-first List directories before files.
-d, --dirs-only List directories only.
-I, --exclude [patterns] Exclude files that match the pattern. | separates alternate patterns. Wrap your entire pattern in double quotes. E.g. `"node_modules|coverage".
-L, --max-depth <n> Max display depth of the directory tree.
-r, --reverse Sort the output in reverse alphabetic order.
-F, --trailing-slash Append a '/' for directories.
-h, --help output usage information
复制代码
tree -d 只显示文件夹;
- tree -L n 显示项目的层级。n表示层级数。比如想要显示项目三层结构,可以用tree -l 3;
- tree -I pattern 用于过滤不想要显示的文件或者文件夹。比如你想要过滤项目中的vendor文件夹,可以使用tree -I "vendor";
- tree > tree.md 将项目结构输出到tree.md这个文件。
举例:如果我们要显示某个项目下3层的所有文件结构,同时又过滤node_modules文件夹,最后输出到tree.md,可以这么写
$ tree -L 3 -I "vendor" > tree.md
复制代码
结果:tree.md
news_watch_notice
├── cmd
│ ├── news_watch_notice.go
│ └── qrcode
│ └── qrcode.jpg
├── conf
│ └── conf.conf
├── dis
│ ├── news_watch_notice
│ └── qrcode
│ └── qrcode.jpg
├── Dockerfile
├── Makefile
├── pkg
│ ├── mail
│ │ └── mail.go
│ ├── reptile
│ │ └── reptile.go
│ └── wechat
│ └── wechat.go
├── qrcode
│ └── qrcode.jpg
├── README.md
├── tree.md
├── utils
│ └── common_utils.go
复制代码
微信公众号
github