jrara
jrara

Reputation: 16991

How to copy files in S3 from folder to another recursively using bash script?

I have a folder structure like this in my AWS S3 bucket named myfiles:

mainfolder/
|-- current
`-- error
    `-- files
        |-- files1
        `-- files2

I'm trying to copy all the files under files1 and files2 folders to current, so that resulting structure shows like this:

mainfolder/
|-- current
|   |-- files1
|   `-- files2
`-- error
    `-- files
        |-- files1
        `-- files2

I'm trying to do this using simple bash for loop like:

BUCKET=myfiles
for dir in $(aws s3 ls s3://$BUCKET/mainfolder/error/files/ | awk '{print $2}'); do
    aws s3 cp $i s3://$BUCKET/mainfolder/current/$i
done

but I'm getting errors:

The user-provided path 0 does not exist.

How this could be done?

Upvotes: 1

Views: 2593

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 269490

You can use:

aws s3 sync s3://my-bucket/mainfolder/error/files/ s3://my-bucket/mainfolder/current/

or:

aws s3 cp --recursive s3://my-bucket/mainfolder/error/files/* s3://my-bucket/mainfolder/current/

See: sync — AWS CLI Command Reference

Upvotes: 2

Related Questions