概要
ちょっとした小ネタです。
コマンドラインでファイルサイズを出力したいときって
$ ls -l total 7220 -rwxr-xr-x 1 gitpod gitpod 2056769 Oct 20 02:52 build-normal -rwxr-xr-x 1 gitpod gitpod 1409024 Oct 20 02:52 build-with-ldflags -rwxr-xr-x 1 gitpod gitpod 2056769 Oct 20 02:52 build-with-trimpath -rwxr-xr-x 1 gitpod gitpod 1409024 Oct 20 02:52 build-with-trimpath-ldflags -rwxr-xr-x 1 gitpod gitpod 452852 Oct 20 02:52 build-with-upx
とかすると、ファイルサイズが表示されますよね。
でも、ちょっとノイズが多い。以下のような形でほしいときがあります。特にスクリプト処理したいときとか。
2056769 build-normal 1409024 build-with-ldflags 2056769 build-with-trimpath 1409024 build-with-trimpath-ldflags 452852 build-with-upx
上の ls の結果を、cut とか sed とか awk とかで加工してやれば勿論いけるのですが、wc コマンド使ってもできます。
(cut とか sed とかって慣れていないとサクっと使えないんですよね・・・)
wc コマンドには -c
というオプションがあって、バイト数を表示してくれます。ロングオプションは --bytes
です。
んで、これを使うと
$ ls -1 | xargs wc -c 2056769 build-normal 1409024 build-with-ldflags 2056769 build-with-trimpath 1409024 build-with-trimpath-ldflags 452852 build-with-upx 7384438 total
と表示できます。
一点だけ、惜しい点があって、必ず最終行に合計が出てくることです。
なので、これを除去してやる
$ ls -1 | xargs wc -c | head -n -1 2056769 build-normal 1409024 build-with-ldflags 2056769 build-with-trimpath 1409024 build-with-trimpath-ldflags 452852 build-with-upx
これで、ファイルサイズとファイル名のみになりました。
過去の記事については、以下のページからご参照下さい。
- いろいろ備忘録日記まとめ
サンプルコードは、以下の場所で公開しています。
- いろいろ備忘録日記サンプルソース置き場