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