一、 引言
最近在整理項目文檔時需要對項目工程目錄結構進行說明,於是瞭解到了windows自帶tree命令可以一鍵生成命令樹,但其功能有限(無法設置忽略某文件),想要更完整的目錄樹的話可以使用Cmder命令工具以及tree-node-cli工具包
二、Cmder
這個工具主要面向Windows用戶,畢竟Linux,Mac OS 系統的命令已經夠用了。
- cmder不是一個獨立的工具,應該說是一系列工具包的集合,包括Conemu、clink、git for windows等,足夠簡單好用,且支持多欄顯示,多個tab運行,功能十分強大。
2.1 下載
- 下載地址
- 它分爲兩個版本
cmder_mini
: 功能簡單,很小巧,只有4M多,主要是cmd和powershellcmder
:功能強大,包含了git、powershell、bash、chocolatey、Cygwin、SDK等功能
- 下載下來後是一個壓縮包,直接解壓即可打開
Cmder.exe
使用
2.2 下載後的配置
要想方便的快速打開它,推薦將Cmder把它加入環境變量,通過服務註冊後,可以右鍵 打開Cmder.exe
- 把安裝路徑添加到環境變量
- 添加cmder到右鍵菜單:在Cmder.exe對應目錄下,添加到環境變量後,運行cmd, 並輸入該命令:
Cmder.exe /REGISTER ALL
, 這樣右擊的時候cmder就會直接跳轉到該路徑下,而無需cd
2.3 更多配置
如果想要切換主題(默認是monocai),設置中文菜單,常用快捷鍵等,可以參考文章:cmder 配置使用指南
三、Tree 命令
一鍵生成目錄結構的工具,windows系統的CMD自帶tree命令這裏不贅述了,功能單一不能滿足需求,所以這裏還是推薦實測比較好用的
tree-node-cli
或者tree-cli
。
3.1 tree-cli
3.1.1 安裝
npm install tree-cli
3.1.2 使用
- 查看可用命令參數
treee --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.
- 輸出目錄
tree -l 3 -o out.txt -d
-l n
:顯示層級-o
: 輸出到文件
3.2 tree-node-cli
3.2.1 安裝
- 如果之前安裝過
tree-cli
這樣的工具,需要先卸載,因爲它們都使用的是tree --option
命令
#安裝tree-node-cli模塊包
npm install -g tree-node-cli
3.2.2 使用
- 查看可用命令參數:
# 查看命令
λ 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|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 -L 3 -I "unpackage" > tree.md
tree -L n
: 顯示項目的層級。n表示層級數。比如你想要顯示項目的2層結構,可以用tree -l 2tree -I pattern
:用於過濾不想要顯示的文件或者文件夾。比如你想要過濾項目中的node_modules
文件夾,可以使用tree -I "node_modules"
,如果想要過濾多個目錄文件,也可以tree -I "node_modules|public|test_*"
,最後一個使用到正則匹配,這樣以test_開頭的文件夾都不會被顯示出來,目錄與目錄之間用豎線隔開,中間不要有空格tree > tree.md
: 將項目結構輸出到tree.md這個文件與在windows DOS的tree命令是一樣的,但是在DOS終端下無法使用類似linux下的一些參數,進行過濾操作注意事項
: 依賴於node,需要全局安裝tree-node-cli
包
最後
- 更多文章參考: 使用tree生成目錄樹結構, cmder 配置使用指南