Skip to content

Instantly share code, notes, and snippets.

@britweb-tim
Last active April 11, 2016 12:28
Show Gist options
  • Save britweb-tim/75fdf8850947d763b331e5d54ab06545 to your computer and use it in GitHub Desktop.
Save britweb-tim/75fdf8850947d763b331e5d54ab06545 to your computer and use it in GitHub Desktop.
Script to delete old files from amazon s3
#!/bin/bash
# Usage: ./deleteOld "bucketname" "30 days"
s3cmd ls $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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment