Skip to content

Instantly share code, notes, and snippets.

@radut
Forked from JProffitt71/s3cmdclearfiles
Created May 24, 2021 13:14
Show Gist options
  • Save radut/6cb357f5ae1fb81b0c194da063281fc1 to your computer and use it in GitHub Desktop.
Save radut/6cb357f5ae1fb81b0c194da063281fc1 to your computer and use it in GitHub Desktop.

Revisions

  1. @JProffitt71 JProffitt71 created this gist Feb 17, 2014.
    19 changes: 19 additions & 0 deletions s3cmdclearfiles
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    #!/bin/bash

    # Usage: ./s3cmdclearfiles "bucketname" "30d"

    s3cmd ls s3://$1 | grep " DIR " -v | while read -r line;
    do
    createDate=`echo $line|awk {'print $1" "$2'}`
    createDate=`date -j -f "%Y-%m-%d %H:%M" "$createDate" +%s`
    olderThan=`date -j -v-$2 +%s`
    if [[ $createDate -lt $olderThan ]]
    then
    fileName=`echo $line|awk {'print $4'}`
    if [[ $fileName != "" ]]
    then
    printf 'Deleting "%s"\n' $fileName
    s3cmd del "$fileName"
    fi
    fi
    done;