Oxana Grey
Oxana Grey

Reputation: 387

how to iterate bash script to run command?

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 ?

Upvotes: 0

Views: 46

Answers (1)

Flavius Condurache
Flavius Condurache

Reputation: 303

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

Upvotes: 2

Related Questions