サブディレクトリごとの使用容量を一覧で出す
satkakuです。
先日、サーバー内のhogeディレクトリ内のサブディレクトリごとの使用容量を一覧で出す必要があったので、そのメモです。
% tree . ├── sample1 │ ├── test1.txt │ ├── test2.txt │ └── test3.txt ├── sample2 │ ├── test1.txt │ ├── test2.txt │ └── test3.txt └── sample3 ├── test1.txt ├── test2.txt └── test3.txt
上記のようなディレクトリ構造があったときに、sample1、sample2、sample3の使用容量を一覧で出します。
中身のファイルそれぞれの使用容量を見たいだけなら、treeコマンドでもいけます。
% tree -ash . ├── [ 170] sample1 │ ├── [ 8] test1.txt │ ├── [ 8] test2.txt │ └── [ 8] test3.txt ├── [ 170] sample2 │ ├── [ 8] test1.txt │ ├── [ 8] test2.txt │ └── [ 8] test3.txt └── [ 170] sample3 ├── [ 8] test1.txt ├── [ 8] test2.txt └── [ 8] test3.txt
「-s」を付けるとサイズも出してくれます。
ですが、今回はサブディレクトリごとの使用容量を見たいので、duコマンドを使います。
% du -sh ./* 12K ./sample1 12K ./sample2 12K ./sample3
「-s」で集計だけ出してくれます。後はパイプでsortを繋げたりして、見やすく出力したりします。
……ですが、今回は「*」のキーが壊れていたことにより、このやり方は使えませんでした。そういうこともあります。仕方が無いので「*」を使わないようにします
% find . -d -maxdepth 1 -exec du -sh {} \; 12K ./sample1 12K ./sample2 12K ./sample3 36K .
findコマンドは「-maxdepth」で探索する深さを指定できるので、そこで1を指定し、その結果を「-exec」で繋いでduに渡します。これで「*」を回避できました。
別にだからなんだというわけでもありませんが、「*」のキーが壊れるとめんどくさいので、「*」キーは労りましょう。