Skip to content

Instantly share code, notes, and snippets.

@bduggan
Created February 28, 2018 17:57
Show Gist options
  • Save bduggan/1435b3f23943222a658d93dbf996697a to your computer and use it in GitHub Desktop.
Save bduggan/1435b3f23943222a658d93dbf996697a to your computer and use it in GitHub Desktop.
#!/bin/sh
# du.sh -- find usage of an s3 bucket and directory (recursively)
#
# Usage:
#
# du.sh <bucket> <directory>
#
# requires jq, perl, aws-cli
BUCKET=$1
DIR=$2
[ -z "$DIR" ] && echo "usage: du.sh <bucket> <directory>" && exit
aws s3api list-objects --bucket $BUCKET --prefix $DIR \
| jq -r '.Contents[] | [ .Size, .Key ] | @tsv' \
| perl -lan \
-e '
@parts = split("/",$F[1]);
for (0..@parts-1) {
$du{join "/",@parts[0..$_]} += $F[0]
};
END {
printf "%10s $_\n",$du{$_},$_
for sort { +$du{$b} <=> +$du{$a} } keys %du
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment