Skip to content

Instantly share code, notes, and snippets.

@nahmedpk
Last active August 9, 2022 10:14
Show Gist options
  • Save nahmedpk/138764a2daebd7da8765bb4934fd2612 to your computer and use it in GitHub Desktop.
Save nahmedpk/138764a2daebd7da8765bb4934fd2612 to your computer and use it in GitHub Desktop.
AWS CLI | Bash Script | Rename files - File With Specific Pattern in a Directory
#!/bin/bash
S3_BUCKET="bucket-storage";
S3_SOURCE_DIR="invoices";
#S3_DEST_DIR="receipts"; //extra
S3_FILENAME_SEARCH="invoice";
S3_FILENAME_REPLACE="receipt";
# Sample Folder Structure
# invoices
# - invoice-3790-1598369636.pdf
# Destination Folder Structure
# receipts
# - receipt-2182-1597744575.pdf
# This script is independent of file extension and destination directory
aws s3 ls "s3://${S3_BUCKET}/${S3_SOURCE_DIR}" --recursive | awk '{ if($3 >0) print $4}' | while read FILE_WITH_PATH ; do
if [[ "${FILE_WITH_PATH}" == *"$S3_FILENAME_SEARCH"* ]]; then
newfile="$(echo ${FILE_WITH_PATH} | sed -e "s/${S3_FILENAME_SEARCH}/${S3_FILENAME_REPLACE}/g")"; # invoices/invoice-183123.pdf ---> receipts/receipt-183123.pdf
#Inspired by
aws s3 mv "s3://${S3_BUCKET}/${FILE_WITH_PATH}" "s3://${S3_BUCKET}/${newfile}" 2>&1 | tee -a move_status.txt
# if you want to move into any other directory
# you can alter the variable `newfile` and remove the directory then alter `aws s3 mv` 2nd argument to move into destination directory
fi
done
@nahmedpk
Copy link
Author

nahmedpk commented Jan 4, 2021

Requirements

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment