以下の内容はhttps://takuya-1st.hatenablog.jp/entry/2017/02/13/233823より取得しました。


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

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




以上の内容はhttps://takuya-1st.hatenablog.jp/entry/2017/02/13/233823より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14