AWS S3 CLI move files with a new name

Viewed 4680

I have millions of images that need to be renamed. All files have a 'C' in part of the filename that needs to be changed to a lower 'c'.

File name pattern:

  • ACD_112_2P-001-1C.jpg should be changed to ACD_112_2P-001-1c.jpg
  • ACD_112_2P-002-2C.jpg should be changed to ACD_112_2P-002-2c.jpg
  • ACD_112_2P-003-3C.jpg should be changed to ACD_112_2P-003-3c.jpg

How can I use AWS CLI with some kinda wild card to achieve this?

Source and destination buckets will be the same.

3 Answers

Using aws cli and bash you can rename multiple files like so:

$ for f in $(aws s3api list-objects --bucket sample-bucket-1 --prefix "" --delimiter "" | grep replaceme | cut  -d ":" -f 2 | tr -d , | tr -d \";
do aws s3 mv s3://sample-bucket-1/$f s3://sample-bucket-1/${f/replaceme/replaced};
done

Where 'replaceme' is the part of the filename you wish to replace and 'replaced' is what you want to insert in the filename.

The following will extract the filename from the response:

 cut  -d ":" -f 2 | tr -d , | tr -d \"

If the files are within folders in a bucket place the folder name in prefix:

--prefix "folder_name/sub_folder_name"

Edited from here: http://gerardvivancos.com/2016/04/12/Single-and-Bulk-Renaming-of-Objects-in-Amazon-S3/

great it works! you are missing a final ) for the loop for

$ for f in $(aws s3api list-objects --bucket sample-bucket-1 --prefix "" --delimiter "" | grep replaceme | cut -d ":" -f 2 | tr -d , | tr -d \"); do aws s3 mv s3://sample-bucket-1/$f s3://sample-bucket-1/${f/replaceme/replaced}; done

$ for f in $(aws s3api list-objects --bucket sample-bucket-1 --prefix "" --delimiter "" | grep replaceme | cut -d ":" -f 2 | tr -d , | tr -d \"; do aws s3 mv s3://sample-bucket-1/$f s3://sample-bucket-1/${f/replaceme/replaced}; done

Related