文章目录
一、背景说明
最近在学习Vue开发,无奈的是生成的项目文件有点多,不方便查看。那么在windows系统中是否有什么命令可以输出目录的树形结构呢?
二、输出目录树
2.1、cmd自带的tree命令
第一种输出目录树的方法是使用windows命令行自带的tree命令:
tree . /F
优点:
系统自带,无需额外安装。
缺点:
- 打印的目录树比较丑
- 功能单一。要么打印所有目录,要么打印所有文件。
- 当打印所有文件时,无法排除无需打印的目录。如果目录中有像.git、node_modules这种不需要输出,且内部文件还很多的目录,就会打印出非常多的信息。
2.2、node中的tree-cli
使用npm安装tree-cli:
npm install -g tree-cli
为了和自带的 tree
命令区分,所以需要用 treee
执行 tree-cli 命令。
打印目录树:
treee . --ignore 'node_modules'
错误写法:
treee . -ignore "node_modules"
错误点:
-ignore应当写成:
--ignore
请注意,这里有一个通用规则:
-
后面接的是一个字母。如-a
--
后面接的是单词或词组。如--version
或是directoryFirst
2.3、我的需求
我需要打印满足如下条件的目录树:
打印vue项目的目录树,排除如下目录:
node_modules
、.git
和.vscode
;
递归打印所有子目录中的文件
打印时,目录中的目录显示在文件的前面
命令如下:
treee . -a --directoryFirst -l 3 --ignore "node_modules, .git, .vscode"
命令说明:
-a
:默认情况下,只打印目录,不打印目录中的文件。使用该参数会打印所有文件。这里的a
是all
的简写。-l 3
:--ignore "node_modules, ..."
:忽略指定的目录,不打印--directoryFirst
:当目录中既有子目录又有文件时,所有的目录显示在最前面。
注意:
- 默认情况下,打印的是当前目录(
即命令执行时当前所在的目录
)- 默认情况下,只打印目录中的子目录。例如:目录下有dir1和3个文件,默认打印的目录树只有dir1
三、问题
3.1、tree-cli的参数有哪些?
答:执行下面的命令:
treee --help
可以打印出该命令的全部参数:
OPTIONS:
--help, -h
outputs a verbose usage listing.
--version
outputs the version of tree-cli.
--debug
show debug info.
--ignore
ignores directory or file you specify.
--base
specify a root directory. Relative path from cwd root and absolute path are both acceptable.
--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.
--directoryFirst
list directories first.
-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.
参数说明表(部分)
参数 | 作用 |
---|---|
--help / h | 输出帮助信息 |
--version | 输出tree-cli版本信息 |
--ignore | 忽略指定的目录或是文件,不对齐进行打印 |
--fullpath | 不管是目录还是文件,都显示全路径。如目录:E:\Java\interview\docs ,文件:E:\Java\interview\面试题.pdf |
-a | 显示目录中的所有文件。默认只打印目录 |
--directoryFirst | 如果目录中既有文件又有目录,目录永远在最前面 |
-f | 在目录后面追加/ ,如:目录bookmarks/ ;socket文件后面追加= ;FIFOs后面追加| |
-l | 目录树遍历的最大深度,后接数字。如 -l 3 。个人猜测字母l 是level 的缩写 |
-o | 将目录树输出到文件。后接文件名,如 -o xx.txt 则是将目录树保存到文件xx.txt中了。这个参数可以放在其他参数的前面。o 是output 的缩写 |
3.2、windows系统中有socket文件和FIFOs吗?
答:这两个都是Linux系统的概念,在windows中不存在。
四、总结
本文讲解了两种打印目录树的方法:一种是使用cmd自带的tree命令,另一种是使用node组件tree-cli;这里建议使用第二种方式打印目录树。
借助 tree-cli 可以打印比较美观的目录树,并且可以排除无需打印的目录(可排除多个)。通过参数 -l
可以指定目录遍历的深度,而参数 -a
则会打印出所有的文件。
参考文章:
https://www.jianshu.com/p/7002eee46561
https://cloud.tencent.com/developer/article/1704247
相关链接:
https://markdown.com.cn/extended-syntax/tables.html#%E5%AF%B9%E9%BD%90