Home > Blockchain >  AWS S3: Remove same file in many folders
AWS S3: Remove same file in many folders

Time:10-28

I have this situation on my AWS S3 bucket

My bucket
├───Product 1
│       sales_Product 1_1.pdf
│       sales_Product 1_2.pdf
│       prod1.pdf
│       prod2.pdf
│
├───Product 2
│       sales_Product 2_1.pdf
│       sales_Product 2_2.pdf
│       prod1.pdf
│       prod2.pdf
│
└───Product 3
        sales_Product 3_1.pdf
        sales_Product 3_2.pdf
        prod1.pdf
        prod2.pdf

My question is: How to delete prod1.pdf and prod2.pdf using recursive option?

I tried to use aws s3 rm s3://My bucket/ --recursive --exclude "*" --include "prod1.pdf" --include "prod2.pdf" but does not working

CodePudding user response:

You need to add * in the object name as they can be in any tree:

aws s3 rm s3://$Bucket --recursive --exclude '*' --include '*/prod1.pdf' --include '*/prod2.pdf'

> aws s3 rm s3://mknappe-tmp --recursive --exclude '*' --include "*/prod1.pdf" --include "*/prod2.pdf"
delete: s3://mknappe-tmp/a/prod1.pdf
delete: s3://mknappe-tmp/b/prod2.pdf
delete: s3://mknappe-tmp/c/prod1.pdf
delete: s3://mknappe-tmp/a/prod2.pdf
delete: s3://mknappe-tmp/c/prod2.pdf
delete: s3://mknappe-tmp/b/prod1.pdf
> aws s3 ls s3://mknappe-tmp/a/
2022-10-27 09:16:25      11623 prod3.pdf
  • Related