title: Amazon S3 Copy files after date and with regex
body: <p>I'm trying to copy some files from S3 sourceBucket to targetBucket, but I need to filter by date and by prefix.
  I wish it could be done with AWS CLI, but at the moment I'm stuck with <a href="https://docs.aws.amazon.com/cli/latest/reference/s3api/list-objects-v2.html" rel="nofollow noreferrer">list-object</a> or with cp command.</p>
  <p>I can filter correctly with</p>
  <pre><code>aws s3api list-objects-v2 --bucket sourceBucket --query 'Contents[?(LastModified &gt; `2021-09-01`)]' --prefix &quot;somePrefix_&quot;
  </code></pre>
  <p>With the CP I can copy the files, but only by prefix</p>
  <pre><code>aws s3 cp s3://sourceBucket/ s3://targetBucket/ --recursive --include &quot;somePrefix&quot; --exclude &quot;*&quot;
  </code></pre>
  <p>I tried to come up with some ideas using the header <code>--x-amz-copy-source-if-modified-since</code> but it looks like you can use it with the command <code>aws s3api copy-object</code> and it copies one item at a time (<a href="https://docs.aws.amazon.com/cli/latest/reference/s3api/copy-object.html" rel="nofollow noreferrer">doc</a>).
  I read some answers/docs and I think I understood che cp command doesn't filter by date, but only by prefix.</p>
  <p>Do you have any idea on how to solve this?
  Thank you in advance!</p>