それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

ファイルサイズを取得し、変数に格納する

rubypython からすぐ出来るんですが、bashでやると面倒ですね

ruby

File.size( 'path/to/file' )

python

import os 
os.path.getsize('path/to/file')

bash

どうやるの? ls -l だと余計なものが多いし。。。まさか、cut してgrep する〜?awkする?

いくつかあるけど stat が良さそう

stat -c %s path/to/file

コレを使って

size=$(stat -c %s path/to/file)

まとめ

ほかにも du -k ls -l wc -c なども考えられる。

ぱっと思いついただけで以下の通り。

ls -l path/to/file | cut -d ' '  -f 5
du --apparent-size --block-size=1  path/to/file
wc -c  < path/to/file
find path/to/file --printf=%s
ruby -e 'puts  File.size  ARGV[0]' path/to/file
  • du だとブロックサイズを意識するのが面倒ですね。
  • ls は cut と組み合わせるのが面倒ですね。
  • rubyワンライナーも面倒ですねぇ
  • find は stat とやってること変わらないから面倒ですねぇ。引数位置とかも。
  • wc はありですねぇ

参考資料

http://unix.stackexchange.com/questions/16640/how-can-i-get-the-size-of-a-file-in-a-bash-script