背景
- README.mdファイルなど、ディレクトリの解説情報を記載する時に、ディレクトリの階層構造を表示したいことが多々あります。
例
./
├── tree_01
├── tree_02
└── tree_03
- ですが、手作業で階層構造を記載するのは結構手間がかかります。
- 自動でフォルダの階層構造を出力してくれるコマンドを探したところ、treeコマンドがあったので使い方をシェアします。
目標
- treeコマンドでディレクトリの階層構造を自動で出力できるようになる。
前提環境
- macOS 12.4
- arm64(M1)
- Homebrew 3.6.5
treeコマンドのinstall
$brew install tree
ディレクトリの階層図を出力するコマンド
- 上記のような、ディレクトリの階層図は以下のtreeコマンドで出力することができます。
#tree -L 出力したい階層 "構成図の作成を開始するpath" /f > "出力ファイルの保存先"/保存ファイル名.txt
$tree -L 2 "./" /f > "../"/tree.md
- 上記の例では、カレントディレクトリに
tree.md
という名前で階層図が出力されます。(以下例)
tree.md
./
├── tree_01
├── tree_02
└── tree_03
3 directories, 0 files
- 出力したい拡張子を
txt
に変更すればテキストファイルで階層図を出力できます。
参考資料
個人ブログ