The aws s3 commands in the AWS CLI handle everyday Amazon S3 work: creating and removing buckets, uploading and downloading files, moving objects and syncing whole directories. The reference below lists the core commands. The sections after it cover setup, folder operations, safe syncing, useful options, common errors and fixes, and two corrections to the original list.
List Buckets: To list all your S3 buckets.
aws s3 ls
List Objects in a Bucket: To list all objects in a specific bucket.
aws s3 ls s3://[bucket-name]/
Create a New Bucket: To create a new S3 bucket.
aws s3 mb s3://[bucket-name]
Delete a Bucket: To delete an S3 bucket. The bucket must be empty before it can be deleted.
aws s3 rb s3://[bucket-name]
Upload File to Bucket: To upload a file to an S3 bucket.
aws s3 cp [file-name] s3://[bucket-name]/[destination-path]
Download File from Bucket: To download a file from an S3 bucket.
aws s3 cp s3://[bucket-name]/[file-name] [local-destination-path]
Delete File from Bucket: To delete a file from an S3 bucket.
aws s3 rm s3://[bucket-name]/[file-name]
Sync Local Directory with Bucket: To synchronize the contents of a local directory with an S3 bucket.
aws s3 sync [local-directory-path] s3://[bucket-name]
Sync Bucket with Local Directory: To synchronize the contents of an S3 bucket with a local directory.
aws s3 sync s3://[bucket-name] [local-directory-path]
Move File: To move a file from one S3 location to another.
aws s3 mv s3://[bucket-name]/[source-file-name] s3://[bucket-name]/[destination-file-name]Code language: JavaScript (javascript)
Prerequisites and a quick setup check
The commands above need AWS CLI version 2, credentials, and IAM permissions for the S3 actions you plan to use. Before running anything against a real bucket, confirm which identity and region the CLI is using.
# Check the CLI version (should report aws-cli/2.x)
aws --version
# Configure access keys, default region and output format
aws configure
# Or use AWS IAM Identity Center (SSO)
aws configure sso
# Confirm which account and identity the CLI is acting as
aws sts get-caller-identityCode language: PHP (php)
Prefer short-lived credentials from IAM Identity Center or an IAM role over long-lived access keys. The AWS Identity Center guide shows how to set this up. To use a named profile, add --profile name to any command, or set AWS_PROFILE.
Note: Bucket names are globally unique across all AWS accounts. They must be 3 to 63 characters long and use only lowercase letters, numbers, dots and hyphens. If you get BucketAlreadyExists, another account already owns that name.
Corrections to the command list
Most of the commands above are correct. Two descriptions are misleading enough to cause trouble in practice.
Listing all objects. aws s3 ls s3://bucket-name/ only shows the top level of the bucket. Objects inside "folders" appear as PRE prefix/ lines, and their contents are not listed. To list every object, add --recursive:
aws s3 ls s3://my-bucket/
# PRE logs/
# 2026-01-10 09:12:45 1024 index.html
aws s3 ls s3://my-bucket/ --recursive --human-readable --summarize
# 2026-01-10 09:12:45 1.0 KiB index.html
# 2026-01-11 14:03:10 2.3 MiB logs/app.log
#
# Total Objects: 2
# Total Size: 2.3 MiBCode language: PHP (php)
Deleting a non-empty bucket. The list above says the bucket must be empty first. That is true for a plain rb, but --force deletes all current objects and then the bucket in one step. If versioning is or ever was enabled, --force does not remove old object versions or delete markers, so the bucket delete still fails. Remove those versions with aws s3api delete-objects or a lifecycle rule, or empty the bucket from the S3 console.
# Deletes every object, then the bucket. This cannot be undone.
aws s3 rb s3://my-bucket --forceCode language: PHP (php)
Working with folders: –recursive, –exclude and –include
S3 has no real directories. A "folder" is just a key prefix such as logs/. Without --recursive, cp, mv and rm work on a single object. To act on everything under a prefix, add --recursive.
# Upload a whole directory
aws s3 cp ./site s3://my-bucket/site/ --recursive
# Delete everything under a prefix (preview first with --dryrun)
aws s3 rm s3://my-bucket/tmp/ --recursive --dryrun
aws s3 rm s3://my-bucket/tmp/ --recursive
# Copy only .log files: exclude everything, then include the pattern
aws s3 cp ./logs s3://my-bucket/logs/ --recursive --exclude "*" --include "*.log"
# Copy between buckets (the transfer stays inside S3)
aws s3 cp s3://source-bucket/data/ s3://dest-bucket/data/ --recursiveCode language: PHP (php)
Filters are applied in order, and a later filter overrides an earlier one. That is why --exclude "*" comes before --include "*.log". If you reverse them, nothing is copied. Quote the patterns so the shell does not expand them.
If the destination of a single-file cp ends in /, the original file name is kept, so aws s3 cp report.pdf s3://my-bucket/docs/ creates docs/report.pdf. Without the trailing slash, the destination is treated as the full object key.
Using sync safely
sync copies only files that are new or changed. When uploading, a file is copied if it does not exist at the destination, if its size differs, or if the local file is newer. sync works in one direction only, and by default it never deletes anything.
# Preview what would change
aws s3 sync ./site s3://my-bucket/site --dryrun
# Mirror exactly: also delete destination files that no longer exist locally
aws s3 sync ./site s3://my-bucket/site --delete
# Skip unwanted files
aws s3 sync ./site s3://my-bucket/site --exclude ".git/*" --exclude "*.tmp"Code language: PHP (php)
Note: Always run --delete with --dryrun first. Swapping the source and destination by mistake can wipe a local directory or a bucket prefix.
Useful options and extra commands
| Option or command | What it does | Example |
|---|---|---|
--region | Sets the bucket region, for example when creating a bucket outside your default region | aws s3 mb s3://my-bucket --region eu-west-1 |
--storage-class | Uploads directly to a cheaper storage class | aws s3 cp backup.tar s3://my-bucket/ --storage-class STANDARD_IA |
--sse | Requests server-side encryption (AES256 or aws:kms) | aws s3 cp file s3://my-bucket/ --sse aws:kms |
--dryrun | Shows what would happen without changing anything | aws s3 rm s3://my-bucket/old/ --recursive --dryrun |
presign | Creates a temporary download URL. The default lifetime is 3600 seconds and the maximum is 7 days | aws s3 presign s3://my-bucket/report.pdf --expires-in 900 |
- (stdin/stdout) | Streams data without writing a temporary file | tar czf - ./data | aws s3 cp - s3://my-bucket/data.tgz |
Large files are uploaded in parts automatically. For many small files or a fast network link, you can raise the number of parallel transfers:
aws configure set default.s3.max_concurrent_requests 20
aws configure set default.s3.multipart_threshold 64MB
aws configure set default.s3.multipart_chunksize 16MBCode language: CSS (css)
The aws s3 commands are high-level wrappers. Bucket settings such as versioning, policies, lifecycle rules and public access blocks are handled by the lower-level aws s3api commands, for example aws s3api put-bucket-versioning.
Common errors and fixes
| Error | Likely cause | Fix |
|---|---|---|
Unable to locate credentials | No profile or environment credentials are configured | Run aws configure or aws sso login, or set AWS_PROFILE |
AccessDenied / 403 Forbidden | The IAM policy, bucket policy or KMS key policy does not allow the action | Check s3:ListBucket on the bucket ARN, s3:GetObject and s3:PutObject on arn:aws:s3:::bucket/*, and KMS permissions for encrypted objects |
BucketAlreadyExists | Another account already owns the name | Choose a more specific name, for example with a company or account prefix |
IllegalLocationConstraintException | The request region does not match the bucket or endpoint region | Pass the correct --region |
BucketNotEmpty | rb was run on a bucket that still has objects or versions | Use rb --force, or delete the old versions first in versioned buckets |
NoSuchBucket | Typo in the name, wrong account or profile | Check with aws s3 ls and aws sts get-caller-identity |
Best practices
- Use
--dryrunbefore anyrm --recursive,mv --recursiveorsync --delete. - Enable versioning on important buckets so overwrites and deletes can be recovered.
- Keep S3 Block Public Access turned on. Share individual files with
presigninstead of making them public. - New buckets disable ACLs by default (Object Ownership set to bucket owner enforced). Control access with IAM and bucket policies, not
--acl. - Grant least-privilege IAM permissions limited to specific buckets and prefixes.
- For traffic from private subnets, use an S3 gateway VPC endpoint to avoid NAT costs. See the VPC endpoint guide.
Frequently asked questions
What is the difference between aws s3 and aws s3api?
aws s3 gives you high-level, file-style commands such as cp, sync and rm, with recursion and multipart uploads built in. aws s3api maps one-to-one to the S3 API and is used for bucket configuration such as versioning, policies and lifecycle rules.
How do I list all files in an S3 bucket including subfolders?
Run aws s3 ls s3://bucket-name/ --recursive. Add --human-readable --summarize to see readable sizes plus the total object count and total size.
Does aws s3 sync delete files?
Not by default. It only adds and updates files at the destination. Add --delete to remove destination files that no longer exist in the source, and preview the result with --dryrun first.
How do I delete an S3 bucket that is not empty?
Use aws s3 rb s3://bucket-name --force, which deletes all objects and then the bucket. In a versioned bucket, the old versions and delete markers must also be removed before the bucket can be deleted.
How can I share an S3 file without making it public?
Generate a presigned URL with aws s3 presign s3://bucket/key --expires-in 3600. Anyone with the URL can download the object until it expires, which can be at most 7 days.
Related tutorials
- More AWS tutorials
- AWS Tutorials: AWS Identity Center Guide
- AWS Tutoriaols: A Comprehensive Guide to AWS VPC Endpoint Services
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals
This blog gives a clear and practical introduction to AWS S3 commands with useful examples. I like how it explains basic operations like creating buckets, uploading files, and managing data in a simple way. Itโs a helpful read for beginners who want to understand S3 using the AWS CLI.