在windows终端里面使用tree命令

  有时候我们需要为我们的项目生成一个树状结构图,windows提供了一个tree命令可供我们使用,但是windows提供的tree命令太过于鸡肋,让我们是又爱又恨,只提供了两个参数供我们使用,不像Linux系统提供的tree命令那么功能强大,深受大家的喜爱,那么对于Windows系统,我们是不是可以放弃了,这里我给大家提供几个方法,方便在Windows系统下面使用这个功能。

Windows下面原生的tree的使用

直接使用

1
2
3
4
5
6
7
F:\website
λ tree
卷 新加卷 的文件夹 PATH 列表
卷序列号为 EC5C-5B89
F:.
├─css
└─lib

这里可以看到输出了website文件下面的两个目录的名字,对于文件夹下面的文件以及子目录里面的文件并没有输出,显然这不是我们想要的,那么我们使用帮助命令看一下,是否有其它参数可供我们选择

1
2
3
4
5
6
7
8
F:\website
λ tree /?
以图形显示驱动器或路径的文件夹结构。

TREE [drive:][path] [/F] [/A]

/F 显示每个文件夹中文件的名称。
/A 使用 ASCII 字符,而不使用扩展字符。

获取全部目录及文件

在上面的命令中我们看到第一条命令可以显示文件夹中文件的名称,似乎可以达到我们的要求,下面我们试一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
F:\website
λ tree /F
卷 新加卷 的文件夹 PATH 列表
卷序列号为 EC5C-5B89
F:.
│ index.html

├─css
│ banner.css
│ main.css

└─lib
banner.js
jquery.js

可以看到确实按照我们想要的结构输出了,但是只有两个命令的使用并不能满足我们日常的需要,比如我们想忽略某个文件,想把生成的树状结构输出的一个文件中又该如何操作,请接着往下看

使用基于nodetree-cli或者treer两个包

要想使用这些命令,我们需要做一些准备

  • nodejs安装,可以戳这里(建议使用LTS的稳定版本,另nodejs自带了npm安装包管理器)

  • cmder,一个替代windows的强大命令终端,安装及使用教程可以戳这里

tree-cli的使用

1、首先安装此模块包

1
npm install -g tree-cli

2、使用此命令

我们先开启cmder,并开启一个bash窗口来操作(cmder默认是cmd视窗,可以根据自己需要来设置默认视窗)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
λ tree --help                                                                        

List contents of directories in tree-like format.

tree - list contents of directories in tree-like format

Tree is a recursive directory listing program that
produces a depth indented listing of files.
With no arguments, tree lists the files in the
current directory. When directory arguments are
given, tree lists all the files and/or directories
found in the given directories each in turn. Upon
completion of listing all files/directories found,
tree returns the total number of files and/or
directories listed.

USAGE

tree <options>

OPTIONS:

--help outputs a verbose usage listing.
--version outputs the version of tree-cli.
--debug show debug info.
--ignore ignores directory or file you specify.
--fullpath prints the full path prefix for each file.
--noreport omits printing of the file and directory report at the end of the tree listing and omits printing the tree on console.
-a all files are printed. By default tree does not print hidden files (those beginning with a dot '.'). In no event does tree print the file system constructs '.' (current directory) and '..' (previous directory).
-d list directories only.
-f append a '/' for directories, a '=' for socket files and a '|' for FIFOs
-i makes tree not print the indentation lines, useful when used in conjunction with the -f option.
-l max display depth of the directory tree.
-o send output to filename.

EXAMPLE:

$ tree

$ tree -l 2, -o out.txt --ignore [node_modules, test] -d --noreport

可以看到tree-cli为我们提供了很多参数命令供我们使用,现在我们来使用一下,我们在根目录创建README.md文件,并使用tree命令生成树状结构图并输出到此文件

1
2
λ tree -l 3, -o README.md  --ignore 'README.md' --noreport
▁ Finish writing to file: F:\website\README.md

上面我们使用了命令显示3级的深度,并忽略掉README.md文件本身,并省略列表末尾的目录报告,现在我们打开文件看一下(这里我们使用了参数-l,是因为该tree命令默认只显示深度层级为一级)

1
2
3
4
5
6
7
8
F:\website
├── css
| ├── banner.css
| └── main.css
├── index.html
├── lib
| ├── banner.js
| └── jquery.js

可以看到命令执行成功,其它命令不在逐一演示,这些命令可以完成我们日常的大部分需求了

tree-node-cli

1、首先安装此模块包

PS:如果之前安装了tree-cli,需要先卸载,防止命令使用上的冲突

1
2
3
4
#卸载之前安装的模块包
npm uninstall -g tree-cli
#安装tree-node-cli模块包
npm install -g tree-node-cli

2、使用此命令

依然是先查看该命令提供的一些参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
λ 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|lcov".
-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

可以看到提供的一些参数跟在linux系统里面使用tree命令提供的参数较为一致(linux系统的使用用户应该比较喜欢这个),只是命令少了一点,目前此模块作者还在持续更新,期待更多的功能开放出来

下面我们使用命令来操作一下

1
2
3
4
5
6
7
8
9
10
λ tree
website
├── css
│ ├── banner.css
│ └── main.css
├── index.html
├── lib
│ ├── banner.js
│ └── jquery.js
└── README.md

可以看到此命令默认显示所有层级,如果你的目录层级结构较深并且文件较多,可能会造成时间读取较长或者命令终端假死,所有请大家在使用之前先看下该命令所提供的一些参数,灵活使用命令进行目录结构输出

Tree for Windows工具

这是GnuWin32提供的一套工具,该工具提供了一套强大的tree命令以供在windows下面使用,使用Windows自带终端即可,目前该工具以停止维护多年,使用上请慎重。

以上,就是安利给大家在Windows上怎么使用tree命令来生成树状结构图,以完成我们在日常工作中的需要,如有不对的地方,望指出修正,谢谢!