-
-
Save techact/c13c7956125f684051174b1d7652ba87 to your computer and use it in GitHub Desktop.
Delete files older than n in S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Usage: . ./awss3deleteolderthan.sh [bucket] [path] "20 minutes ago" | |
endpoint="https://ams3.digitaloceanspaces.com" | |
aws s3 ls s3://$1/$2 --endpoint $endpoint --recursive | while read -r line; | |
do | |
createDate=`echo $line|awk {'print $1" "$2'}` | |
createDate=`date -d"$createDate" +%s` | |
olderThan=`date --date "$3" +%s` | |
if [[ $createDate -lt $olderThan ]]; then | |
fileName=`echo $line|awk {'print $4'}` | |
if [[ $fileName != "" ]] | |
then | |
aws s3 rm s3://$1/$fileName --endpoint $endpoint | |
fi | |
fi | |
done; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment