Skip to content

Instantly share code, notes, and snippets.

@almoorthi
Created March 13, 2023 10:27
Show Gist options
  • Save almoorthi/6bf43cb4f90ca112b5e89a1acfa4b22e to your computer and use it in GitHub Desktop.
Save almoorthi/6bf43cb4f90ca112b5e89a1acfa4b22e to your computer and use it in GitHub Desktop.
delete aws s3 files in a bucket older than specified days
#!/bin/bash
# Usage: ./deletes3files.sh "bucketname" "30 days"
olderThan=$(date -d "-$2" +%s)
s3cmd ls s3://$1 | while read -r line;
do
createDate=$(echo $line|awk {'print $1" "$2'})
createDate=$(date -d "$createDate" +%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