LoginSignup
41

More than 5 years have passed since last update.

du -h などの結果を簡単にソートする

Last updated at Posted at 2016-04-18

du -h とするとサイズが 10M のように表示されて見やすいです。しかし、結果をソートをしたい時に sort -n でうまくソートできません。

$ du --max-depth=1 -h | sort -nr
640K    ./.git
128K    ./config
116K    ./app
 81M    .
 73M    ./vendor
 32K    ./bin
 16K    ./public
4.8M    ./tmp
4.0K    ./test
4.0K    ./db
4.0K    ./.bundle
2.7M    ./log
  0B    ./lib

ところが 2009 年にリリースされた GNU coreutils 7.5 以降では、sort -h で楽にソートできるようです。ちなみに -r は降順です。

$ du --max-depth=1 -h | sort -hr
 81M    .
 73M    ./vendor
4.8M    ./tmp
2.7M    ./log
640K    ./.git
128K    ./config
116K    ./app
 32K    ./bin
 16K    ./public
4.0K    ./test
4.0K    ./db
4.0K    ./.bundle
  0B    ./lib

便利ですね!!!

Mac

ちなみに Mac では・・・。

brew install coreutils
du -d 1 -h | gsort -hr

ls

ちなみに sort -k を使えば ls のようなカラムが複数ある結果もソートできます。

ls -lh | sort -hr -k 5,5

実は ls -S でサイズの逆順にソートできるから不要なのですが・・・。

ls -lhS

参考

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
41