Last active
December 3, 2019 11:12
Star
You must be signed in to star a gist
#AWS script to move a range of files from an #S3 bucket
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 | |
# e.g. | |
# ./s3move.sh -d Images/all/ "15-36-58" "15-54-27" Captures/iPhone6/ | |
DEBUG= | |
BUCKET=screenshots | |
PREFIX=Screenshot_ | |
EXT=".png" | |
while getopts ":db:p:e:" opt; do | |
case ${opt} in | |
d ) | |
DEBUG=echo | |
;; | |
b ) | |
BUCKET=$OPTARG | |
;; | |
p ) | |
PREFIX=$OPTARG | |
;; | |
e ) | |
EXT=$OPTARG | |
;; | |
\? ) | |
echo "Invalid option: $OPTARG" 1>&2 | |
echo "Usage: s3move.sh [-d] [-b BUCKET_NAME] [-p PREFIX] [-e EXTENSION] SOURCE FIRST LAST DESTINATION" | |
echo " -d: dryrun" | |
;; | |
: ) | |
echo "Invalid option: $OPTARG requires an argument" 1>&2 | |
;; | |
esac | |
done | |
shift $((OPTIND -1)) | |
SOURCE=$1 | |
FIRST=$2 | |
LAST=$3 | |
DESTINATION=$4 | |
echo Bucket: $BUCKET | |
echo Prefix: $PREFIX | |
echo Source: $SOURCE | |
echo Extension: $EXT | |
echo First: $FIRST | |
echo Last: $LAST | |
echo Destination: $DESTINATION | |
aws s3 ls "s3://$BUCKET/$SOURCE" | sed -E 's/.*'"$PREFIX"'(.+)'"$EXT"'/\1/' | while read T | |
do | |
if [[ ! "$T" > "$LAST" ]] && [[ ! "$T" < "$FIRST" ]]; then | |
$DEBUG aws s3 mv "s3://$BUCKET/$SOURCE$PREFIX$T$EXT" "s3://$BUCKET/$DESTINATION$PREFIX$T$EXT" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment