1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

プロジェクト構造図、手打ちしないで。treeってコマンドがあるよ。

Posted at

tree とは

ファイル/ディレクトリの階層構造を “木” のかたちで一撃表示してくれる超便利コマンド。プロジェクトの全体像を掴みたいとき、資料に構造を貼り付けたいときに重宝するよ。


0. ざっくりまとめ

OS インストール可否 代表的なコマンド 備考
macOS brew install tree tree Homebrew 必須
Linux Debian/Ubuntu: sudo apt install tree
RHEL/Fedora: sudo dnf install tree
Alpine: apk add tree
tree ほとんどのディストリでパッケージあり
Windows (CMD/PowerShell) 標準搭載 tree /F 一部オプションが *nix 版と異なる
Git Bash / WSL pacman -S tree など tree *nix 版と同じオプションが使える

1. 環境構築

1‑1. macOS

Homebrew が入っている前提。

brew install tree

入ってるか確認

which tree
# /opt/homebrew/bin/tree みたいに返ってくれば OK

1‑2. Linux

パッケージマネージャで一発。

  • Debian/Ubuntu 系

    sudo apt update && sudo apt install tree
    
  • Fedora/RHEL 系

    sudo dnf install tree           # 旧環境なら yum install tree
    
  • Alpine

    sudo apk add tree
    

1‑3. Windows

  • CMD / PowerShellインストール不要、標準搭載
    (ただし *nix 版のフルオプションは使えない)

  • Git Bash / MSYS2

    pacman -S tree           # Git for Windows 同梱の pacman で
    
  • WSL なら Ubuntu などの手順で OK。


2. 実行

OS コマンド例 ひとこと
macOS / Linux / WSL / Git Bash bash tree デフォルトは 2 階層。
Windows (CMD/PowerShell) cmd tree /F /F でファイルも表示(ディレクトリだけなら /A)。
階層制限(共通) bash tree -L 2 2 階層までに絞る。Windows 版にはなし。

実行するとその場に “木” を ASCII で描いた一覧がダーッと出るだけ。


3. 出力を保存する

3‑1. 初級編(とりあえず保存)

tree > tree.txt

→ カレントに tree.txt ができる。

3‑2. 中級編(タイムスタンプ付き)

# macOS/Linux/WSL
tree > "$(date '+%y%m%d-%H%M%S')_tree.txt"

# Windows (PowerShell)
tree /F | Out-File ("$(Get-Date -Format 'yyMMdd-HHmmss')_tree.txt")

→ 日付入りファイル名なので履歴管理しやすい。

3‑3. 上級編(専用フォルダに自動保存)

mkdir -p tree_logs
tree -a > "tree_logs/$(date '+%y%m%d-%H%M%S')_tree.txt"

PowerShell なら:

$dir = "tree_logs"; if (!(Test-Path $dir)) { New-Item -ItemType Directory -Path $dir }
tree /F | Out-File ("$dir/$(Get-Date -Format 'yyMMdd-HHmmss')_tree.txt")

4. 便利オプション(*nix 版)

オプション 説明 Windows 対応
-a 隠しファイルも表示 ×
-L <n> 表示階層を <n> に制限 ×
`-I 'pat1 pat2'` 除外パターン(正規表現) ×
-d ディレクトリのみ表示 ×
-s ファイルサイズも表示 ×
-D 最終更新日時も表示 ×
--du ディレクトリごとの合計サイズ ×

Windows 版の注意

  • /F … ファイルまで表示
  • /A … ASCII 文字で描画(Shift‑JIS/Unicode 問題を避けたいとき)。
  • /? でヘルプが出るけど、Linux 版ほど豊富なオプションはない。

5. よくある Tips

  • grep と合わせ技

    tree -a | grep -i '\.jpg$'      # JPG だけ抽出
    
  • VS Code の拡張 “CodeTree”
    出力を Markdown にして README に貼り付けたいときに便利。

  • Git の差分確認に

    tree -a > before.txt
    # …ファイル操作…
    tree -a > after.txt
    diff -u before.txt after.txt
    

まとめ

  • Mac / Linux はパッケージ入れて tree
  • Windows は標準 tree /F、ただし機能少なめ。
  • 保存は > でリダイレクト、タイムスタンプで履歴管理。
  • *nix 版の豊富なオプションでフィルタや階層制限もお手のもの。

これさえ覚えておけば、
「ファイル構造見せて!」と言われた瞬間に 一撃&即共有、キマるね。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?