Selective file download in AWS CLI
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Selective download in the AWS CLI usually means copying only certain S3 objects instead of the entire bucket or prefix. The standard tool is aws s3 cp with --recursive, --exclude, and --include filters, or a direct cp command when you already know the exact object key.
Download One Exact Object
If you already know the full S3 key, use a simple copy command.
This is the cleanest form of selective download because no filtering logic is needed.
Download A Filtered Set Of Objects
When you need multiple files under a prefix, the AWS CLI documentation recommends combining recursive copy with --exclude and --include filters.
This downloads only objects ending in .log under the logs/ prefix.
Why --exclude "*" Comes First
The AWS CLI command reference points out an easy-to-miss rule: by default, all files are included. That means --include alone does not narrow the transfer.
The normal selective pattern is:
- exclude everything first
- re-include the files you want
So this works:
But this usually does not do what people expect:
Without the initial exclusion, all objects remain eligible.
Combine Multiple Patterns
You can include more than one pattern.
This is useful when a local folder should receive only a subset of image or report types.
Filter By Prefix When Possible
If the desired objects already live under a clean prefix, use the narrowest S3 path you can. It reduces scanning work and makes the command easier to reason about.
Then add filters only if the prefix still contains more objects than you want.
Use --dryrun Before Large Transfers
Selective filters are easy to get wrong. The safest habit is to preview the transfer first.
That prints what would be copied without downloading anything.
Include And Exclude Work For Uploads Too
The same filter pattern works in the opposite direction when copying from local files to S3. That is useful when you want one mental model for both upload and download filtering rather than learning two separate command styles.
cp Versus sync
aws s3 sync can also use include and exclude filters, but cp is often clearer when the task is explicitly "download these matching objects." Use sync when you want destination contents to mirror a source tree more closely.
For one-off targeted retrieval, cp is usually simpler.
Common Pitfalls
A common mistake is using --include without first excluding everything. According to the AWS CLI documentation, --include only re-includes objects that were excluded.
Another mistake is starting from the bucket root when a narrower prefix is available. That makes filtering slower and easier to misread.
It is also easy to forget --dryrun. On large buckets, a bad pattern can pull far more data than intended.
Summary
- Use direct
aws s3 cpwhen you know the exact object key. - Use
--recursive --exclude "*" --include "pattern"for selective multi-object downloads. - Start from the narrowest useful prefix in S3.
- Use multiple
--includefilters when needed. - Run with
--dryrunfirst when the transfer scope is nontrivial.
Related reading
- Self-Terminating AWS EC2 Instance?
- self referencing aws security groups
- Sending email via AWS SES within AWS Lambda function
- Sending html content in AWS SNSSimple Notification Service emails notifications
- Sending SMS with Amazon AWS services PHP
- Server Sent Events In a Kubernetes Cluster
- Serverless framework deployment error You're not authorized to access this resource
- Serverless Framework with AWS Lambda error Cannot find module

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.