一起聊聊 Mac 環(huán)境如何快速生成目錄結(jié)構(gòu)樹
前言
有時(shí)候當(dāng)我們?cè)趯?README 的的時(shí)候需要對(duì)項(xiàng)目的結(jié)構(gòu)進(jìn)行展示的話,這個(gè)時(shí)候我們就可以很好的利用 Mac 自帶的工具 tree,來幫我們快速的生成。
1. 安裝 tree
brew install tree
2. 參數(shù)介紹
參數(shù)解讀:
-a # 顯示所有文件,包括隱藏文件(以 “.” 點(diǎn)開頭的文件 )
-d # 只顯示目錄
-f # 只顯示每個(gè)文件的全路徑
-i # 不顯示樹枝,常與-f參數(shù)配合使用
-L # level 遍歷目錄的最大層數(shù),level 為大于0的正整數(shù)
-F # 在執(zhí)行文件、目錄、Socket符號(hào)鏈接、管道名稱等不同類型文件的結(jié)尾,各自加上“*”、 "/"、"="、"@"、"|"號(hào)、類似ls命令的-F選項(xiàng)
3. demo 目錄
# 測(cè)試項(xiàng)目的文件層級(jí)關(guān)系
.
└── src
└── components
└── common
├── FootCell
│ └── index.vue
├── Pagination
│ └── index.vue
├── Table
│ └── index.vue
└── TitleCell
└── index.vue
4. 生成指定文件
進(jìn)入到要生成 tree 目錄:
tree [-d] -L ${number} > ${文件名[.后綴]}
$ tree -L 3 > test1.md
└── src
└── components
└── common
3 directories
$ tree -d -L 3 > test2.md
├── src
│ └── components
│ └── common
3 directories, 3 files
5. 不帶任何參數(shù),直接調(diào)用 tree
tree # 會(huì)在終端直接輸出上述demo結(jié)果
6. 以樹形結(jié)構(gòu)顯示目錄下的所有內(nèi)容(-a 的功能)
├── .DS_Store
└── src
├── .DS_Store
└── components
├── .DS_Store
└── common
├── .DS_Store
├── FootCell
│ └── index.vue
├── Pagination
│ └── index.vue
├── Table
│ └── index.vue
└── TitleCell
└── index.vue
7 directories, 8 files
7. 只列出目錄下第一層目錄的結(jié)構(gòu)(-L 功能)
一層 tree -L 1
└── src
二層 tree -L 2
└── src
└── components
三層 tree -L 3
└── src
└── components
└── common
8. 顯示所有目錄(但不顯示文件)
不帶路徑 tree -d
顯示當(dāng)前文件的目錄
KaKa:test hhdd$ tree -d
# 結(jié)果
.
└── src
└── components
└── common
├── FootCell
├── Pagination
├── Table
└── TitleCell
7 directories
帶路徑 tree -d ${路徑}
顯示指定路徑下的文件的目錄
bash
KaKa-3:test hhdd$ tree -d /Users/hhdd/Desktop/test
# 輸出結(jié)果
/Users/hhdd/Desktop/test
└── src
└── components
└── common
├── FootCell
├── Pagination
├── Table
└── TitleCell
7 directories
帶參數(shù) tree -dL ${number} || tree
-d -L ${number}
-d 參數(shù)只顯示目錄,-L 參數(shù)顯示層數(shù)
KaKa-3:test hhdd$ tree -dL 1
# 結(jié)果
.
└── src
1 directory
9. -f選項(xiàng)和-i選項(xiàng)的使用
使用-f選項(xiàng)可顯示完整的路徑名稱,使用-i選項(xiàng)則不顯示樹枝部分,示例代碼如下:
-f 可顯示完整的路徑名稱
KaKa-3:test hhdd$ tree -d -L 2 -f
# 結(jié)果
.
└── ./src
└── ./src/components
2 directories
-i 不顯示樹枝部分
# 輸出結(jié)果
.
./src
./src/components
2 directories
10. 使用 tree 命令 區(qū)分 目錄和文件的方法(常用)
使用-F參數(shù)會(huì)在目錄后面添加 “/ ”,方便區(qū)分目錄
形式 tree -L {路徑}]
有路徑
KaKa-3:test hhdd$ tree -L 1 -F /Users
# 輸出結(jié)果
/Users
├── Guest/
├── Shared/
└── hhdd/
3 directories, 0 files
無路徑參數(shù)
KaKa-3:test hhdd$ tree -L 1 -F
# 輸出結(jié)果
.
└── src/
1 directory, 0 files
對(duì)比不加 -F
KaKa-3:test hhdd$ tree -L 1
# 輸出結(jié)果
.
└── src
1 directory, 0 files
總結(jié)
全文總結(jié):
這篇文章主要介紹了在 Mac 環(huán)境中如何使用自帶工具 tree 來生成目錄結(jié)構(gòu)樹。包括安裝 tree 的方法,詳細(xì)講解了各種參數(shù)如a、-d、-f、-i、-L、-F 的功能和用法,并通過示例展示了不同參數(shù)組合下生成的目錄結(jié)構(gòu)效果,還介紹了如何生成指定文件以及區(qū)分目錄和文件的常用方法。
重要亮點(diǎn):
- 安裝 tree:使用 brew install tree 命令安裝。
- 參數(shù)功能:
- -a:顯示所有文件,包括隱藏文件。
- -d:只顯示目錄。
- -f:顯示每個(gè)文件的全路徑。
- -i:不顯示樹枝。
- -L:控制遍歷目錄的最大層數(shù)。
- -F:在不同類型文件結(jié)尾加上特定符號(hào)以區(qū)分。
- 生成指定文件:進(jìn)入相應(yīng)目錄,通過 tree [-d] -L {文件名[.后綴]} 生成。
- 不同參數(shù)組合效果:如 -L 控制顯示層數(shù),f 與 -i 配合使用等。
- 區(qū)分目錄和文件:使用 -F 參數(shù)在目錄后添加 “/ ”方便區(qū)分。
原文地址:Mac 環(huán)境快速生成目錄結(jié)構(gòu)樹https://juejin.cn/post/6980215157213364237
原文作者:拒絕996