← All posts
2 min read

Filter a recursive S3 copy

#aws#cli#s3#storage
📑 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" \
  --dryrun

A 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" \
  --dryrun

This 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/" --recursive

Remove only the uploaded prefix, not unrelated bucket content.

Next, we will learn Use S3 sync delete without surprises.

Official AWS CLI reference