1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

treeコマンドのすゝめ

Posted at

treeコマンドとは

tree構造でファイル構成を見やすく表示してくれるコマンド。

$ tree -L 2
.
├── README.md
├── __pycache__
│   ├── database.cpython-39.pyc
│   └── uniqlo_spider.cpython-39.pyc
├── bin
│   ├── README.md
│   ├── chromedriver
│   └── headless-chromium
├── crawler
│   ├── crawler
│   └── scrapy.cfg
├── database.py
├── main.py
├── model
│   ├── __pycache__
│   └── item.py
├── requirements.txt
├── test_spider.py
└── venv
    ├── bin
    ├── include
    ├── lib
    └── pyvenv.cfg

10 directories, 13 files

5 directories, 5 files

pecoと組み合わせるとかなり便利。
pr_10-20.gif

インストール

macユーザーなので、brewで入れる。
brewが入っていない方は、ここからインストール。

$ brew install tree

使い方

-Lオプションで、深さを指定。

$ tree -L 1
.
├── README.md
├── __pycache__
├── bin
├── crawler
├── database.py
├── main.py
├── model
├── requirements.txt
├── test_spider.py
└── venv

5 directories, 5 files

-aオプションで、隠しファイルも表示。

tree -a -L 1
.
├── .DS_Store
├── .env
├── .gcloudignore
├── .git
├── .gitignore
├── README.md
├── __pycache__
├── bin
├── crawler
├── database.py
├── main.py
├── model
├── requirements.txt
├── test_spider.py
└── venv

--duオプションでデータ量を表示。
-hオプションでデータ量をM,K表示に。
--sortオプションでソート。

$ tree --du -h -a --sort=size
...

    │           └── [ 50K]  zope.interface-5.4.0.dist-info
    │               ├── [   4]  INSTALLER
    │               ├── [2.0K]  LICENSE.txt
    │               ├── [ 40K]  METADATA
    │               ├── [8.3K]  RECORD
    │               ├── [ 110]  WHEEL
    │               ├── [   5]  namespace_packages.txt
    │               └── [   5]  top_level.txt
    └── [ 106]  pyvenv.cfg

 310M used in 771 directories, 6626 files

便利な使い方

MBytes,GBytes単位の大きなファイルを絞り込んで表示。

$ tree --du -h -a --sort=size | grep -e M] -e G]
.
└── [3.4M]  test_repository
    ├── [1.2M]  .git
    │   ├── [1.2M]  objects
    │   │   └── [1.2M]  pack
    │   │       └── [1.2M]  pack-
xxxx.pack
    ├── [2.0M]  assets
    │   ├── [2.0M]  fonts
    │   │   └── [2.0M]  Roboto

pecoで検索かける。pecoのインストールは、こちら

$ tree --du -h -a --sort=size | peco

pr_10-20.gif

aliaseに追加しておくと便利。

zshrc
# tree aliases
alias trees="tree --du -h -a --sort=size | grep -e M] -e G]"
alias tp="tree --du -h -a --sort=size | peco"

注意点

-Lを指定した際、指定された深さまでしか探索されず、データサイズも探索されたファイルの合計でしかない。

$ tree --du -h -L 1
.
├── [1.1K]  License
├── [2.2K]  README.md
├── [ 18K]  association.go
├── [ 480]  callbacks <- 深さ1の場合は、480Bytes
├── [7.9K]  callbacks.go

$ tree --du -h -L 2
.
├── [1.1K]  License
├── [2.2K]  README.md
├── [ 18K]  association.go
├── [ 60K]  callbacks <- 深さ1の場合は、60KBytes!! ここのサイズが増えてる。。。
│   ├── [ 11K]  associations.go
│   ├── [3.3K]  callbacks.go
...
│   └── [9.0K]  update.go

[番外編] tree入れたく無いけど、tree使いたい

$ pwd;find . | sort | sed '1d;s/^\.//;s/\/\([^/]*\)$/|--\1/;s/\/[^/|]*/|  /g'

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?