LoginSignup
2
2

More than 3 years have passed since last update.

ディレクトリ構成を出力するtreeコマンドをMacOSで実行してみた

Last updated at Posted at 2020-02-09

treeコマンドを導入することで、以下のようにディレクトリ構成を一目で確認することが可能です。

% tree
.
├── sample1-1.txt
├── sample1-2.txt
└── sampleDir2
    ├── sample2-1.txt
    ├── sample2-2.txt
    ├── sample2-3.txt
    └── sampleDir3
        └── sample3-1.txt

Homebrew導入

Homebrew が入っていない人は、最初に Homebrew をインストールしましょう。

Homebrewのインストール

% /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Homebrewバージョン確認

% brew -v
Homebrew 2.2.5
Homebrew/homebrew-core (git revision 1252; last commit 2020-02-08)
Homebrew/homebrew-cask (git revision d266c; last commit 2020-02-09)

最新バージョンをインストール

% brew update

treeコマンド導入

以下のコマンドを実行してtreeコマンドをインストールします。

treeコマンドインストール

% brew install tree

これで導入は完了。

treeコマンドの使用方法

構成を確認したいフォルダにcdコマンドで移動したあと、treeコマンドを実行します。

% tree [オプション] [ディレクトリ...]

使用例

ディレクトリ sampleDir1 の構造をtreeコマンドで見てみます。
以下の.sampleDir1 を表しています。

% tree
.
├── sample1-1.txt
├── sample1-2.txt
└── sampleDir2
    ├── sample2-1.txt
    ├── sample2-2.txt
    ├── sample2-3.txt
    └── sampleDir3
        └── sample3-1.txt

-dオプション(ディレクトリのみ表示)

% tree -d
.
└── sampleDir2
    └── sampleDir3

-Lオプション(表示する深さを指定)

% tree -L 1
.
├── sample1-1.txt
├── sample1-2.txt
└── sampleDir2

1 directory, 2 files

-Pオプション(条件に一致したファイルを表示)

% tree -P "sample2*"
.
└── sampleDir2
    ├── sample2-1.txt
    ├── sample2-2.txt
    ├── sample2-3.txt
    └── sampleDir3

-uオプション(ファイルの所有者を含めて表示)

% tree -u
.
├── [username ]  sample1-1.txt
├── [username ]  sample1-2.txt
└── [username ]  sampleDir2
    ├── [username ]  sample2-1.txt
    ├── [username ]  sample2-2.txt
    ├── [username ]  sample2-3.txt
    └── [username ]  sampleDir3
        └── [username ]  sample3-1.txt

2 directories, 6 files

-gオプション(ファイルの所有グループを含めて表示)

% tree -g
.
├── [staff   ]  sample1-1.txt
├── [staff   ]  sample1-2.txt
└── [staff   ]  sampleDir2
    ├── [staff   ]  sample2-1.txt
    ├── [staff   ]  sample2-2.txt
    ├── [staff   ]  sample2-3.txt
    └── [staff   ]  sampleDir3
        └── [staff   ]  sample3-1.txt

-sオプション(ファイルのサイズ(バイト数)を含めて表示)

% tree -s
.
├── [          0]  sample1-1.txt
├── [          0]  sample1-2.txt
└── [        192]  sampleDir2
    ├── [          0]  sample2-1.txt
    ├── [          0]  sample2-2.txt
    ├── [          0]  sample2-3.txt
    └── [         96]  sampleDir3
        └── [          0]  sample3-1.txt

-Dオプション(ファイルの更新時刻を含めて表示)

% tree -D
.
├── [Feb  9 16:48]  sample1-1.txt
├── [Feb  9 16:48]  sample1-2.txt
└── [Feb  9 16:49]  sampleDir2
    ├── [Feb  9 16:49]  sample2-1.txt
    ├── [Feb  9 16:49]  sample2-2.txt
    ├── [Feb  9 16:49]  sample2-3.txt
    └── [Feb  9 16:49]  sampleDir3
        └── [Feb  9 16:49]  sample3-1.txt

参考

【 tree 】コマンド――ディレクトリをツリー状に表示する

2
2
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
2
2