1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

複数個あるファイルの総サイズを求める方法

Posted at

複数個あるファイルの総サイズを求める方法を調べたので共有します。

具体的には、次の 2 つのケースについて説明します。

  • カレントディレクトリにあるファイルの総サイズ
  • 複数のディレクトリにあるファイルの総サイズ

カレントディレクトリにあるファイルの総サイズ

この場合は簡単です。

# 詳細を表示する
ls -l
# 2進接頭辞表記に変換する。K,M,Gなどと書かれていますが KiB,MiB,GiBです。
ls -lh

このコマンドで総サイズがわかります。

複数のディレクトリにあるファイルの総サイズ

次は、複数のディレクトリをまたぐ場合です。
この場合は、findでファイルリストを、duでファイルサイズを得て、 awkを使い合計を求めるのが良いでしょう。
対象ディレクトリの共通する親ディレクトリに移動し、次のコマンドを入力します。

find . -type f -exec du {} + | sort -rn | awk 'BEGIN {print "SIZE\tNAME"} {sum+=$1;print $0} END {print sum,"TOTAL"}'

これで直下のディレクトリのファイルの総サイズが取得できます。
カレントディレクトリにあるファイルの総サイズもこのやり方でわかりますが、タイプ数が多いので使うことはないでしょう

更に絞り込みたい場合はfind-nameオプションなどで細かくファイルを指定できます。

1
0
0

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?