Filter a recursive S3 copy
📑 On this page
Part 55 of AWS from Zero. This lesson keeps the scope to one S3 behavior you can verify from the terminal.
What we are learning
Recursive cp supports the same ordered include and exclude filters used by sync.
Before you run it
aws sts get-caller-identity
REGION="ap-south-1"
BUCKET="replace-with-your-private-demo-bucket"Use a private general purpose bucket that you own. Replace every placeholder before running a write or delete command.
The command
aws s3 cp local-site/ "s3://$BUCKET/site/" \
--recursive \
--exclude "*" \
--include "*.html" \
--include "*.css" \
--dryrunA successful configuration command may return no output. Treat inspection as a separate required step.
Inspect the result
aws s3 cp local-site/ "s3://$BUCKET/site/" \
--recursive \
--exclude "*" \
--include "*.html" \
--include "*.css"Run the exact command without --dryrun only after reviewing every proposed upload.
One tiny variation
aws s3 cp local-site/ "s3://$BUCKET/site/" \
--recursive \
--exclude ".git/*" \
--exclude "*.map" \
--dryrunThis variation starts included and removes only known unwanted paths.
Common mistake
Filter order matters, and patterns match relative paths. Test with --dryrun whenever the rule set changes.
Cleanup
aws s3 rm "s3://$BUCKET/site/" --recursiveRemove only the uploaded prefix, not unrelated bucket content.
Next, we will learn Use S3 sync delete without surprises.