Skip to content

Instantly share code, notes, and snippets.

@clarkritchie
Last active July 27, 2023 20:19
Show Gist options
  • Save clarkritchie/0e0287a32ef60f26a6674a4a0569eeef to your computer and use it in GitHub Desktop.
Save clarkritchie/0e0287a32ef60f26a6674a4a0569eeef to your computer and use it in GitHub Desktop.
s3 list, copy, delete
#!/usr/bin/env bash
# quick and dirty script to
# - list buckets matching
# - copy buckets matching
# - remove buckets matching
SEARCH=${1}
ACTION=${2:-"ls"}
if [ -z ${SEARCH} ]; then
echo "Need search base"
exit 1
fi
buckets=$(aws s3api list-buckets --query 'Buckets[?starts_with(Name, `'${SEARCH}'`) == `true`].Name' --output text)
for b in ${buckets[@]}
do
if [[ "${ACTION}" == "ls" ]]; then
echo ${b}
elif [[ "${ACTION}" == "cp" ]]; then
aws s3 cp s3://"${b}" "./${b}" --recursive
elif [[ "${ACTION}" == "rm" ]]; then
read -p "Delete bucket ${b}? Are you sure? " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
aws s3 rb --force s3://${b}
else
echo "skipping delete of bucket ${b}"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment