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?

AWSのS3においてファイルサイズの上位をCLIで見る方法

Posted at

やりたいこと

S3の特定ディレクトリにファイルがたくさん格納されている。
そのファイルのうちサイズが大きいものをCLIで取得したい。

  • S3の特定ディレクトリのファイル一覧が欲しい
  • ファイルサイズの昇順でソートしたい
  • ファイルサイズが大きい上位5件だけほしい

S3においてファイルサイズの上位をCLIで見る方法

aws s3 lsのオプションにはなさそうなので他ののコマンドを合わせて使用する。

$ aws s3 ls s3://{バケット}/{ディレクトリ}/ --recursive | sort -k3nr | head -n 5
2025-05-30 10:10:00      987654321 large-document-1.zip
2025-05-30 10:15:00      876543210 large-document-2.zip
2025-05-31 08:20:00      765432109 large-document-3.zip
2025-05-29 10:25:00      654321098 large-document-4.zip
2025-05-30 02:00:00      543210987 large-document-5.zip

recursive

--recursiveを指定して全部ディレクトリではなくファイルを表示する。
それにより「1.日付」「2.時間」「3.サイズ」「4.ファイル名」が一覧で表示できる

recursive
このオプションを使用すると、指定のディレクトリ内またはプレフィックス内のすべてのファイルやオブジェクトに対してコマンドが実行されます。
引用 : Using high-level (s3) commands in the AWS CLI - AWS Command Line Interface

sort

sortにオプションをつけて「サイズの昇順」に並べる

  • k : ソートのキーになる位置
    • 「k3」で「3.サイズ」を指定する
  • n : 数値として扱う
    • キーになる値を数値として扱うために、「n」を指定する
  • r : 昇順にする

head

headにオプションをつけて「先頭の指定件数を取得する」

  • n : 取得する件数
    • 「n 5」で5件取得する
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?