Skip to content

Instantly share code, notes, and snippets.

@skydiver
Forked from adatta02/gist:4743092
Last active March 12, 2016 02:54
Show Gist options
  • Save skydiver/2b92cc2798f95275940f to your computer and use it in GitHub Desktop.
Save skydiver/2b92cc2798f95275940f to your computer and use it in GitHub Desktop.
Purge old files from S3 with "s3cmd"
#!/bin/bash
# Version: 0.2.0
#
# Usage:
# ./s3purge "bucketname" "30 days"
# ./s3purge "bucketname" "6 hours"
/usr/local/bin/s3cmd ls s3://$1 | while read -r line;
do
createDate=`echo $line|awk {'print $1" "$2'}`
createDate=`date -u -d"$createDate" +%s`
olderThan=`date -u -d"-$2" +%s`
# DEBUG DATES
#echo "$createDate - $olderThan"
if [[ $createDate -lt $olderThan ]]
then
fileName=`echo $line|awk {'print $4'}`
if [[ $fileName != "" ]]
then
# DEBUG FILENAME TO DELETE
# echo $fileName
/usr/local/bin/s3cmd del "$fileName"
fi
fi
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment