Mac で tree コマンドを使えるようにする

tree コマンドは、対象のディレクトリからのディレクトリ構造をツリー形式で表示するコマンド。Mac ではデフォルトで利用できるコマンドではないので、インストールして使えるようにする。

環境

  • macOS Monterey 12.6.3
  • Homebrew 4.2.2

インストール

Homebrew でインストールできる。

% brew install tree

念のためインストール先とバージョンを確認してみる。

% which tree
/usr/local/bin/tree

% ls -al /usr/local/bin/tree 
lrwxr-xr-x  1 gumfum  admin  31  1  3 18:07 /usr/local/bin/tree -> ../Cellar/tree/2.1.1_1/bin/tree

% tree --version
tree v2.1.1 © 1996 - 2023 by Steve Baker, Thomas Moore, Francesc Rocher, Florian Sesser, Kyosuke Tokoro

treeコマンドを使ってみる

tree <ディレクトリ> で、指定したディレクトリをルートとしたツリー構造を表示する。

% tree root_dir
root_dir
├── child_dir
│   ├── child_file_1
│   └── child_file_2
└── root_file

ディレクトリを指定しない場合は、現在のディレクトリをルートとして表示する。ルートは . として表示される。

% cd root_dir 
% tree
.
├── child_dir
│   ├── child_file_1
│   └── child_file_2
└── root_file

-f オプションを付けることで、各ディレクトリとファイルのルートディレクトリからのフルパスを表示できる。

% tree -f root_dir 
root_dir
├── root_dir/child_dir
│   ├── root_dir/child_dir/child_file_1
│   └── root_dir/child_dir/child_file_2
└── root_dir/root_file

-i オプションを付けることで、ツリー構造を表示せずにファイルの一覧のみを表示できる。

% tree -i root_dir 
root_dir
child_dir
child_file_1
child_file_2
root_file

% tree -f -i root_dir
root_dir
root_dir/child_dir
root_dir/child_dir/child_file_1
root_dir/child_dir/child_file_2
root_dir/root_file

他にも色々なオプションがあるが、--help で確認できる通りかなり多いためこの辺りで割愛。