Question:
Need to delete files in Amazon S3 which is older than 7 days, need a shell script to do this, no luck with google search, i found the below url
http://shout.setfive.com/2011/12/05/deleting-files-older-than-specified-time-with-s3cmd-and-bash/
it is not helpful to us, Does someone have script to delete all files older than 7 days?
Answer:
Thanks, John
We have modified code little bit and it is working fine.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
aws s3 ls BUCKETNAME/ | while read -r line; do createDate=`echo $line|awk {'print $1" "$2'}` createDate=`date -d"$createDate" +%s` olderThan=`date --date "7 days ago" +%s` if [[ $createDate -lt $olderThan ]] then fileName=`echo $line|awk {'print $4'}` if [[ $fileName != "" ]] then aws s3 rm BUCKETNAME/$fileName fi fi done; |