输出目录树之命令tree

本文介绍了如何在Windows环境下利用cmd的tree命令和node包tree-cli生成Vue项目目录树,排除node_modules、.git和.vscode等目录,并详细解释了tree-cli的参数使用和socket文件/FIFOs在Windows系统中的适用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、背景说明

最近在学习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:默认情况下,只打印目录,不打印目录中的文件。使用该参数会打印所有文件。这里的aall 的简写。
  • -l 3
  • --ignore "node_modules, ...":忽略指定的目录,不打印
  • --directoryFirst:当目录中既有子目录又有文件时,所有的目录显示在最前面。

注意:

  1. 默认情况下,打印的是当前目录即命令执行时当前所在的目录
  2. 默认情况下,只打印目录中的子目录。例如:目录下有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 。个人猜测字母llevel的缩写
-o将目录树输出到文件。后接文件名,如 -o xx.txt 则是将目录树保存到文件xx.txt中了。这个参数可以放在其他参数的前面ooutput的缩写

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值