EnsekiTT Blog

EnsekiTTが書くブログです。

treeコマンドを使ってディレクトリ構造を人に伝えるためにテキストで表示する話

つまりなにしたの?

Blogを書いていると、こういうディレクトリ構造にしていますよ。って話をしたくなる。
同じ課題を持つ人はいるらしく、そういうのをいい感じにテキストで出力するtreeコマンドを紹介する。
f:id:ensekitt:20180708204519j:plain

対象とする環境

LinuxとかMacOSの開発環境
今回はMacOSの場合を紹介する

インストールする

brew install tree

使ってみる

 % tree
.
├── bar
└── foo
    ├── fuga.txt
    └── hoge

3 directories, 1 file

もしくはファイルパスを指定して

 % tree /Users/your_user/dev
/Users/your_user/dev
├── bar
└── foo
    ├── fuga.txt
    └── hoge

3 directories, 1 file

こんな感じで出てくる

よく使うオプションとか

  • 階層の深さを指定する
 % tree -L 1
.
├── bar
└── foo

2 directories, 0 files
  • ファイルとかディレクトリのサイズをも合わせて表示する
 % tree -s
.
├── [         64]  bar
└── [        128]  foo
    ├── [         12]  fuga.txt
    └── [         64]  hoge

3 directories, 1 file
 % tree -pug
.
├── [drwxr-xr-x who group   ]  bar
└── [drwxr-xr-x who group   ]  foo
    ├── [-rw-r--r-- who group   ]  fuga.txt
    └── [drwxr-xr-x who group   ]  hoge

3 directories, 1 file
 % tree -f
.
├── ./bar
└── ./foo
    ├── ./foo/fuga.txt
    └── ./foo/hoge

3 directories, 1 file
クリエイティブ・コモンズ・ライセンス
この 作品 は クリエイティブ・コモンズ 表示 4.0 国際 ライセンスの下に提供されています。