📖📖📖快速生成文件目录树

前言

有时候我们在写文章的时候为了更好地向读者展示文件目录结构,我们通常会采用文本缩进的方式制作文件目录树,以下是一个示例:

shell 复制代码
|-- mddir
    |-- .gitignore
    |-- AUTHORS
    |-- CHANGES.md
    |-- LICENSE.txt
    |-- logo.svg
    |-- mddir.png
    |-- package.json
    |-- README.md
    |-- public
    |-- src
        |-- mddir.js
        |-- npm-debug.log

这里主要是记录一下快速生成文件目录树的一些技巧

window系统自带命令

window自带tree命令,可以快速打印目录树

tree 命令格式和参数:

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

  • /F 显示每个文件夹中文件的名称。(带扩展名)
  • /A 使用 ASCII 字符,而不使用扩展字符。(如果要显示中文,例如 tree /f /A >tree.txt)
shell 复制代码
WJR ❯ tree /a /F
卷 软件 的文件夹 PATH 列表
卷序列号为 7286-AE25
D:.
|   .gitignore
|   AUTHORS
|   CHANGES.md
|   directoryList.md
|   LICENSE.txt
|   logo.svg
|   mddir.png
|   package.json
|   README.md
|   tree-help.md
|   tree.md
|
+---public
\---src
        mddir.js
        npm-debug.log

可以将目录树输出到tree.md

shell 复制代码
tree /A /F > tree.md

三方工具

以上是 window 中 tree(mac没有自带的tree命令),但是很多时候,这指令似乎有些不够用,比如:

我们在生成目录是希望忽略包文件的目录,有时我们要的并不是所有目录结构,希望可以增加一下条件来输出目录树,那么就可以借助第三方工具

  1. tree-cli
  2. tree-node-cli

tree-cli

文档地址

安装

shell 复制代码
npm install -g tree-cli

执行命令

由于window上有自带的tree命令,所以官方文档上也介绍了,为了避免和系统的 tree 命令冲突,需要用 treee 代替 tree

帮助命令

shell 复制代码
D:\wjr> treee -h


  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, -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.

  EXAMPLE:

  $ tree

  $ tree --directoryFirst -f

  $ tree -l 2, -o out.txt --noreport

  $ tree -l 2, -o out.txt --ignore 'node_modules, test' -d -f'
 

参数大全

shell 复制代码
  --help, -h 输出用法信息
  --version 输出版本号
  --debug debug信息
  --ignore 忽略文件或者文件夹
  --base 指定根目录,支持相对路径或者绝对路径
  --fullpath 每个文件均打印全路径
  --noreport 不打印文件及文件夹的总数
  -a 默认是不打印以'.'或者'..'开头的文件或者文件夹,-a则打印所有包括'.'或者'..'开头的
  -d 只打印文件夹
  --directoryFirst 只打印顶级文件夹
  -f  为目录追加一个"/",为套接字文件追加一个"="和一个"|"表示FIFOs
  -i 不打印缩进线,一般与-f选项一起使用时生成平级目录
  -l 目录树的最大显示深度
  -o 打印内容导成文件

例子

1. 输出目录树

shell 复制代码
WJR ❯ treee
▁
D:\project\wjr_project\mddir
├── AUTHORS
├── CHANGES.md
├── directoryList.md
├── LICENSE.txt
├── logo.svg
├── mddir.png
├── package.json
├── public
├── README.md
├── src
├── tree-help.md
└── tree.md

directory: 2 file: 10

2. 只输出文件夹目录

shell 复制代码
WJR ❯ treee -d                                                                  mddir master  ● 100
▁
D:\project\wjr_project\mddir
├── public
└── src

directory: 2

3. 输出目录树且带.git等以'.'或者'..'的目录树

shell 复制代码
WJR ❯ treee -a                                                                  mddir master  ● 100
▁
D:\project\wjr_project\mddir
├── .git
├── .gitignore
├── AUTHORS
├── CHANGES.md
├── directoryList.md
├── LICENSE.txt
├── logo.svg
├── mddir.png
├── package.json
├── public
├── README.md
├── src
├── tree-help.md
└── tree.md
  1. 打印指定深度的目录树
shell 复制代码
WJR ❯ treee -l 2
▁
D:\project\wjr_project\mddir
├── AUTHORS
├── CHANGES.md
├── directoryList.md
├── LICENSE.txt
├── logo.svg
├── mddir.png
├── package.json
├── public
├── README.md
├── src
|  ├── mddir.js
|  └── npm-debug.log
├── tree-help.md
└── tree.md

directory: 2 file: 12

tree-node-cli

文档地址

安装

ruby 复制代码
$ npm install tree-node-cli
# or globally
$ npm install -g tree-node-cli

执行命令

同样的。由于window上有自带的tree命令,为了避免和系统的 tree 命令冲突,需要用 treee 代替 tree

帮助命令

lua 复制代码
D:\tree-demo> treee -h
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                display help for command

参数大全

css 复制代码
-V, --version 输出版本号
-a, --all-files 打印所有文件,包括隐藏文件
--dirs-first 目录在前,文件在后
-d, --dirs-only 仅列出目录
-I, --exclude [patterns] 排除与模式匹配的文件。用 | 隔开,用双引号包裹。 例如 "node_modules|.git"
-L, --max-depth <n> 目录树的最大显示深度
-r, --reverse 按反向字母顺序对输出进行排序
-F, --trailing-slash 为目录添加'/'
-h, --help 输出用法信息

最后

获取目录树的场景不算多,但是如果是经常写文章的同学,应该会用得比较频繁,这个时候就可以利用这些工具节省一丢丢时间

相关推荐
麒麟而非淇淋34 分钟前
AJAX 入门 day1
前端·javascript·ajax
2401_8581205337 分钟前
深入理解MATLAB中的事件处理机制
前端·javascript·matlab
阿树梢41 分钟前
【Vue】VueRouter路由
前端·javascript·vue.js
随笔写2 小时前
vue使用关于speak-tss插件的详细介绍
前端·javascript·vue.js
史努比.2 小时前
redis群集三种模式:主从复制、哨兵、集群
前端·bootstrap·html
快乐牌刀片882 小时前
web - JavaScript
开发语言·前端·javascript
秋雨凉人心3 小时前
call,apply,bind在实际工作中可以使用的场景
javascript
miao_zz3 小时前
基于HTML5的下拉刷新效果
前端·html·html5
Zd083 小时前
14.其他流(下篇)
java·前端·数据库
哪 吒3 小时前
华为OD机试 - 第 K 个字母在原来字符串的索引(Python/JS/C/C++ 2024 E卷 100分)
javascript·python·华为od