![图片[1]-Windows 上如何使用 tree 生成目录树:详细指南与 SEO 优化 - 搜源站-搜源站](https://www.souyuanzhan.com/wp-content/uploads/2025/03/5878283f8420250302024900.webp)
在撰写博客、项目设计文档时,列出项目的结构树是一项常见需求。比如,呈现项目的目录层级关系,像这样:
plaintext
news_watch_notice
├── cmd //main
├── conf
├── dis
├── Dockerfile
├── Makefile
├── pkg
├── qrcode
├── .travis.yml
├── README.md
├── vendor
├── utils
这不仅能清晰展示项目架构,还方便团队成员理解项目内容,提升协作效率。而tree
命令就是实现这一功能的得力助手,接下来就为大家介绍在 Windows 系统上如何使用它。
使用 Windows 自带的 tree 命令
Windows 系统为我们提供了tree
命令来生成目录树。不过,它的功能相对有限。其基本语法是:TREE [drive:][path] [/F] [/A]
。其中,/F
用于显示每个文件夹中文件的名称,/A
则是使用 ASCII 字符,而不使用扩展字符。
例如,在D:\dev\gopath\src\news_watch_notice
目录下执行tree /a
命令,会得到这样的结果:
plaintext
文件夹 PATH 列表 卷序列号为 989E-DB7ED:
.
+---.idea
+---cmd
| \---qrcode
+---conf
+---dis
| \---qrcode
+---pkg
| +---mail
| +---reptile
| \---wechat
+---qrcode
+---utils
执行tree /f
命令,结果如下:
plaintext
文件夹 PATH 列表 卷序列号为 989E-DB7ED:
.
│ .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
从这两个示例可以看出,虽然它能输出目录结构,但功能确实比较基础。在实际使用中,我们可能会有更多需求,比如忽略某些文件,或者将生成的树状结构输出到文件中,而 Windows 自带的tree
命令在这些方面就显得有些力不从心。
使用基于 Node 的 tree-node-cli
为了满足更复杂的需求,我们可以借助基于 Node 的tree-node-cli
工具。在使用之前,需要做好以下准备工作:
- 安装 Node.js:建议安装长期支持(LTS)的稳定版本,Node.js 自带了 npm 包管理器,方便后续安装其他工具。可以通过官方网站下载安装包进行安装。
- 安装 tree-node-cli:在安装好 Node.js 后,打开命令提示符或终端,执行
npm install -g tree-node-cli
命令,即可全局安装tree-node-cli
。
安装完成后,就可以使用丰富的命令选项了。执行tree --help
,可以查看详细的命令说明:
plaintext
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 "node_modules" > tree.md
。最终生成的tree.md
文件内容大致如下:
plaintext
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
通过以上内容,大家应该对在 Windows 系统上使用tree
命令生成目录树有了更深入的了解,无论是自带的tree
命令,还是功能更强大的tree-node-cli
,都能在不同场景下满足我们的需求。希望这篇文章能帮助你更高效地处理项目目录结构相关的工作。
暂无评论内容