Deleting files older than specified time with s3cmd and bash
Update: Amazon has now made it so you can set expiration times on objects in S3, see more here: https://forums.aws.amazon.com/ann.jspa?annID=1303
Recently I was working on a project where we upload backups to Amazon S3. I wanted to keep the files around for a certain duration and remove any files that were older than a month. We use the s3cmd utility for most of our command line based calls to S3, however it doesn’t have a built in “delete any file in this bucket that is older than 30 days” function. After googling around a bit, we found some python based scripts, however there wasn’t any that was a simple bash script that would do what I was looking for. I whipped this one up real quick, it may not be the best looking but it gets the job done:
#!/bin/bash
# Usage: ./deleteOld "bucketname" "30 days"
s3cmd ls s3://$1 | while read -r line;
do
createDate=`echo $line|awk {'print $1" "$2'}`
createDate=`date -d"$createDate" +%s`
olderThan=`date -d"-$2" +%s`
if [[ $createDate -lt $olderThan ]]
then
fileName=`echo $line|awk {'print $4'}`
echo $fileName
if [[ $fileName != "" ]]
then
s3cmd del "$fileName"
fi
fi
done;Questions or comments about this? Email us at contact@setfive.com.