Home > Back-end >  how to iterate bash script to run command?
how to iterate bash script to run command?

Time:01-14

I'm trying to copy multiple folders in years wise from one s3 bucket to another

aws s3 sync s3://bucket_one/2022XX s3://bucket_two/2022XX (XX is 01 to 12)

I've folders under bucket_one like 202201,202202 so on and for other years too.

Can I create any shell script which runs for months of a years to copy data to bucket_two ?

I'm new in shell script, I know how run loops but place holders using not sure.

Any help ?

CodePudding user response:

You can do that with for loops and variable substitution aka parameter expansion. You can read more about it in the official manual here but personaly I find it easier to read something like this.

Anyway for your specific case you can try something like this:

for month in {01..12}; do
    aws s3 sync s3://bucket_one/2022$month s3://bucket_two/2022$month
done

To debug if something doesn't work you can echo the command out. Ex:

for month in {01..12}; do
    echo "aws s3 sync s3://bucket_one/2022$month s3://bucket_two/2022$month"
done

Hope it helps.

CodePudding user response:

maybe this script can help with that

note : i didn't try it

#!/bin/bash

# Define the year and the months to copy
year=2022
months=(01 02 03 04 05 06 07 08 09 10 11 12)

# Loop through the months
for month in ${months[@]}; do
    # Construct the source and destination S3 paths
    src_path="s3://bucket_one/${year}${month}"
    dst_path="s3://bucket_two/${year}${month}"
    # Run the S3 sync command
    aws s3 sync $src_path $dst_path
done
   
  • Related