Skip to content

Instantly share code, notes, and snippets.

@jacekk
Created March 4, 2019 15:53
Show Gist options
  • Save jacekk/e1a7e80332e7a032ba96378ca64916e7 to your computer and use it in GitHub Desktop.
Save jacekk/e1a7e80332e7a032ba96378ca64916e7 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Usage: ./s3-remove-old-files "bucket-name" "nested-path" 1000
if [ ! "$1" ]; then
echo "1st arg (bucket name) NOT specified !!"
exit 1
fi
if [ ! "$2" ]; then
echo "2nd arg (bucket sublocations) NOT specified !!"
exit 2
fi
if [ ! "$3" ]; then
echo "3rd arg (num of days) NOT specified !!"
exit 3
fi
olderThanTs=`date -d "-$3 days" +%s`
aws s3 ls "$1/$2" --recursive | grep -i zip | while read -r line;
do
createdAtExtracted=`echo $line | awk {'print $1" "$2'}`
createdAtTs=`date -d "$createdAtExtracted" +%s`
fileName=`echo $line | awk {'print $4'}`
if [[ $createdAtTs < $olderThanTs ]];
then
echo "Removing file from '$createdAtExtracted' with name: $1/$fileName"
aws s3 rm "$1/$fileName"
fi
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment