sitateru tech blog: コマンド

sitateru tech blog

シタテルの技術やエンジニアの取り組みを紹介するテックブログです。

ラベル コマンド の投稿を表示しています。 すべての投稿を表示
ラベル コマンド の投稿を表示しています。 すべての投稿を表示

2018年12月27日木曜日

コンソール上でディレクトリの中身を樹形図表示する - tree コマンドの紹介

12月 27, 2018

こんにちは! シタテルエンジニアの諏訪です。

今日はコンソール画面で使う tree コマンドについてご紹介します。

コンソールでディレクトリの中身をみる

ファイルの一覧表示

みなさんはコンソールを使っていてあるディレクトリ内のファイルの一覧がほしいときに
どんなコマンドを打ちますか?

たいていは ls コマンドを使うと思います。

……コンソールを使う人が最初に覚えるコマンドの一つですね!

$ ls
Gemfile        Rakefile    config        lib        public        tmp
Gemfile.lock    app        config.ru    log        storage        vendor
README.md    bin        db        package.json    test

これだと一覧性に欠けてちょっと見づらいので -l オプションを付ける方も多いでしょう。

$ ls -l
total 56
-rw-r--r--   1 p-san  wheel  2210 12 27 12:34 Gemfile
-rw-r--r--   1 p-san  wheel  5360 12 27 12:34 Gemfile.lock
-rw-r--r--   1 p-san  wheel   374 12 27 12:34 README.md
-rw-r--r--   1 p-san  wheel   227 12 27 12:34 Rakefile
drwxr-xr-x  10 p-san  wheel   320 12 27 12:34 app
drwxr-xr-x   9 p-san  wheel   288 12 27 12:34 bin
drwxr-xr-x  16 p-san  wheel   512 12 27 12:34 config
-rw-r--r--   1 p-san  wheel   130 12 27 12:34 config.ru
drwxr-xr-x   3 p-san  wheel    96 12 27 12:34 db
drwxr-xr-x   4 p-san  wheel   128 12 27 12:34 lib
drwxr-xr-x   3 p-san  wheel    96 12 27 12:34 log
-rw-r--r--   1 p-san  wheel    67 12 27 12:34 package.json
drwxr-xr-x   9 p-san  wheel   288 12 27 12:34 public
drwxr-xr-x   3 p-san  wheel    96 12 27 12:34 storage
drwxr-xr-x  11 p-san  wheel   352 12 27 12:34 test
drwxr-xr-x   5 p-san  wheel   160 12 27 12:34 tmp
drwxr-xr-x   3 p-san  wheel    96 12 27 12:34 vendor

ディレクトリ直下のファイルを確認するにはこれでよいのですが

時として、サブディレクトリの中のファイルも同時に確認できると便利なことがあります。

新米エンジニアPさんの奮闘記を見てみましょう。

ファイルを探せ!

登場人物

  • 【Pさん】
      開発チームにジョインしたばかりの新米エンジニア。
      シェルコマンドは cd と ls しか知らないくせに
      やたらとコンソール画面を使いたがる、ちょっとクセのある新人。

Pさん初めてのタスク

Pさんの仕事初日。ソースコードの構成もよくわかっていない中で最初のタスクが飛び込んできました。

── Pさん、早速で悪いけど、404.html をちょっと修正してもらえる?

── は〜い、わかりました〜!

元気よく返事をしたものの、その 404.html とやらは一体どこにあるのでしょう?

開封したての PC にまだエディタすら入れていないPさん。
とりあえずコンソール画面で ls コマンドを打ちまくって手当たり次第に探すことにしました。

── えーっと、エラー画面のソースだから…… view ディレクトリの中にあるんだよねきっと。

$ cd app/views/
$ ls -l
total 0
drwxr-xr-x  2 p-san  wheel  64 12 27 12:34 item
drwxr-xr-x  5 p-san  wheel 160 12 27 12:34 layouts
drwxr-xr-x  2 p-san  wheel  64 12 27 12:34 message
drwxr-xr-x  2 p-san  wheel  64 12 27 12:34 user

ありません。

── おや、、、フォルダしかないじゃん。。。すると layouts の中かな。。。

$ cd layouts/
$ ls -l
total 24
-rw-r--r--  1 p-san  wheel  347 12 27 12:34 application.html.erb
-rw-r--r--  1 p-san  wheel  229 12 27 12:34 mailer.html.erb
-rw-r--r--  1 p-san  wheel   13 12 27 12:34 mailer.text.erb

ありません。

── あれれ。。。あー、さては assets の中だな(錯乱)

$ cd ../../assets/
$ ls -l
total 0
drwxr-xr-x  3 p-san  wheel   96 12 27 12:34 config
drwxr-xr-x  3 p-san  wheel   96 12 27 12:34 images
drwxr-xr-x  8 p-san  wheel  256 12 27 12:34 javascripts
drwxr-xr-x  6 p-san  wheel  192 12 27 12:34 stylesheets

やっぱりありません。

── これはもう素直に find コマンドを使うしか…

$ cd ../../
$ find . -name '404,html'

(何も表示されない)

── 見つから…ない…… あっ、見つからないから 404 なんだ。なんつってハハハ(錯乱)

(注)錯乱したPさんはタイプミスをしていますが、本人気づく様子がありません。

コンソールの敗北

結局Pさんは、cd コマンドを使って 404.html を探すのを諦め
GUI なエディタをインストールしました。

エディタにはファイルを樹形図表示する機能があって
それを使うと一瞬で目的のファイルが見つかったのでした。

── はぁ〜意外と浅いところにあったのね。。。もうコンソールきらい。使うのやめよ。。。

やっぱり GUI ソフトにあるようなファイルの樹形図表示機能はとても便利です。
サブディレクトリも含めてファイルを一覧表示してくれるから。

Mac や Windows を使っていれば Finder や エクスプローラその他 GUI ソフトを使えばいいのですが
クラウドサービス上の仮想マシンに SSH でつなぐ場合など
シェルだけで操作を完結させないといけない場面も多いです。

……上の物語に出てきた新人Pさん、今後の仕事でちょっと苦労しそうですね。

コンソール画面でも、こんなふうに樹形図表示することができたらいいのに。。。

コンソールでディレクトリの中身をみる(再)

ファイルの一覧表示 (樹形図)

問題提起したばっかりですが結論です。樹形図表示、 実はできるんです

ご覧いただきましょう。tree コマンドです。

$ tree
.
├── Gemfile
├── Gemfile.lock
├── README.md
├── Rakefile
├── app
│   ├── assets
│   │   ├── config
│   │   │   └── manifest.js
│   │   ├── images
│   │   ├── javascripts
│   │   │   ├── application.js
│   │   │   ├── cable.js
│   │   │   ├── channels
│   │   │   ├── item.coffee
│   │   │   ├── message.coffee
│   │   │   └── user.coffee
│   │   └── stylesheets
│   │       ├── application.css
│   │       ├── item.scss
│   │       ├── message.scss
│   │       └── user.scss
│   ├── channels
│   │   └── application_cable
│   │       ├── channel.rb
│   │       └── connection.rb
│   ├── controllers
│   │   ├── application_controller.rb
│   │   ├── concerns
│   │   ├── item_controller.rb
│   │   ├── message_controller.rb
│   │   └── user_controller.rb
│   ├── helpers
│   │   ├── application_helper.rb
│   │   ├── item_helper.rb
│   │   ├── message_helper.rb
│   │   └── user_helper.rb
│   ├── jobs
│   │   └── application_job.rb
│   ├── mailers
│   │   └── application_mailer.rb
│   ├── models
│   │   ├── application_record.rb
│   │   └── concerns
│   └── views
│       ├── item
│       ├── layouts
│       │   ├── application.html.erb
│       │   ├── mailer.html.erb
│       │   └── mailer.text.erb
│       ├── message
│       └── user

      :
    (省略)
      :

├── public
│   ├── 404.html
│   ├── 422.html
│   ├── 500.html
│   ├── apple-touch-icon-precomposed.png
│   ├── apple-touch-icon.png
│   ├── favicon.ico
│   └── robots.txt

      :
    (省略)
      :

300 directories, 1311 files

どうですかこの見事な樹形図!!! (グルメリポート風に)

深さを指定する

デフォルトだと上のように、カレントディレクトリから 3-階層目まで表示されます。
これだとちょっと多すぎる(or 少なすぎる)という場合は -L オプションで階層の深さを指定できます。

例: 2-階層目まで表示

$ tree -L 2
.
├── Gemfile
├── Gemfile.lock
├── README.md
├── Rakefile
├── app
│   ├── assets
│   ├── channels
│   ├── controllers
│   ├── helpers
│   ├── jobs
│   ├── mailers
│   ├── models
│   └── views

      :
    (省略)
      :

├── public
│   ├── 404.html
│   ├── 422.html
│   ├── 500.html
│   ├── apple-touch-icon-precomposed.png
│   ├── apple-touch-icon.png
│   ├── favicon.ico
│   └── robots.txt
├── storage

      :
    (省略)
      :

33 directories, 35 files

変更時間順にソートする

-t オプションを指定すればファイルの変更時間順に並べ替えて表示することができます。

$ tree -t
.
├── Gemfile
├── README.md
├── Rakefile
├── config.ru
├── app
│ ├── assets
│ │ ├── config
│ │ │ └── manifest.js
│ │ ├── images
│ │ ├── javascripts
│ │ │ ├── application.js
│ │ │ ├── cable.js
│ │ │ ├── channels

      :
    (省略)
      :

300 directories, 1311 files

ディレクトリを指定する

ls コマンドがそうであるように、対象ディレクトリを指定して表示させることもできます。

$ tree app/assets/
app/assets/
├── config
│   └── manifest.js
├── images
├── javascripts
│   ├── application.js
│   ├── cable.js
│   ├── channels
│   ├── item.coffee
│   ├── message.coffee
│   └── user.coffee
└── stylesheets
    ├── application.css
    ├── item.scss
    ├── message.scss
    └── user.scss

5 directories, 10 files

このほかにもさまざまなオプションを指定できます。

インストール・Tips

For Linux

Linux の場合は yum でインストールします。

yum install tree

インストールできたことの確認:

$ tree --version

バージョンが表示されれば OK 。

For Mac

Mac の場合は homebrew でインストールできます。

$ brew install tree

インストールできたことの確認:

$ tree --version

バージョンが表示されれば OK 。

Tips (文字化けするとき)

Linux だと罫線が文字化けすることがあります。

$ tree
.
tqq 404.html
tqq 422.html
tqq 500.html
tqq apple-touch-icon-precomposed.png
tqq apple-touch-icon.png
tqq favicon.ico
mqq robots.txt

そんなときは --charset=C を指定すると OK です。

$ tree --charset=C
.
├── 404.html
├── 422.html
├── 500.html
├── apple-touch-icon-precomposed.png
├── apple-touch-icon.png
├── favicon.ico
└── robots.txt

おわりに

tree コマンドによるコンソールでの樹形図表示、いかがでしたでしょうか。

便利だなと思った方、ぜひ今日インストールして使ってみてくださいね。

一度インストールしておけばずっと使えます。