Search code examples
linuxamazon-web-servicesamazon-s3copy

copy and rename filenames in aws s3 cli


I need to duplicate files in aws s3 bucket many times and rename these copies iteratively.

these are the file types in my bucket

filename_10yr.txt
filename_10yr.pdf
filename_10yr.xml

I want to duplicate files with filename containing '10yr' many times and rename the copies as

 filename_1yr.txt
filename_1yr.pdf
filename_1yr.xml
filename_2yr.txt
filename_2yr.pdf
filename_2yr.xml 

and so on...

I got an syntax error near unexpected token `find' in the command below. I'm new to linux & aws cli, any pointers on where to place the copy s3 command? Thanks

for i in {1..10}; find . -type f -name '*10yr*' -exec bash -c 'aws s3 cp s3://mybucket/ "$0" "${0/10yr/$iyr}"' {} \; done

Solution

  • If I'm understanding your question correctly, you have a set of files in the bucket that have 10yr in the filename, and for each of those files you want to create nine copies (with the suffix from 1 to 9).

    If so, then in bash you could do something like:

    mybucket="..."
    for item in $(aws s3 ls --recursive "s3://${mybucket}" | awk '$4 ~ /10yr.+$/ {print $4}'); do
      for c in {1..9}; do
        new_filename=$(sed -E "s/[0-9]+(.+)/${c}\1/" <<< "$item")
        aws s3 cp "s3://${mybucket}/${item}" "s3://${mybucket}/${new_filename}"
      done
    done
    

    so if I have file f_10yr.txt in the bucket mybucket, then the output will be:

    copy: s3://mybucket/f_10yr.txt to s3://mybucket/f_1yr.txt
    copy: s3://mybucket/f_10yr.txt to s3://mybucket/f_2yr.txt
    copy: s3://mybucket/f_10yr.txt to s3://mybucket/f_3yr.txt
    copy: s3://mybucket/f_10yr.txt to s3://mybucket/f_4yr.txt
    copy: s3://mybucket/f_10yr.txt to s3://mybucket/f_5yr.txt
    copy: s3://mybucket/f_10yr.txt to s3://mybucket/f_6yr.txt
    copy: s3://mybucket/f_10yr.txt to s3://mybucket/f_7yr.txt
    copy: s3://mybucket/f_10yr.txt to s3://mybucket/f_8yr.txt
    copy: s3://mybucket/f_10yr.txt to s3://mybucket/f_9yr.txt