Programster's Blog

Tutorials focusing on Linux, programming, and open-source

AWS CLI S3 Cheatsheet

It's much faster to interface with S3 files through the CLI than it is to use the web browser. Here is a cheatsheet of useful S3 based CLI commands. In order to use these commands, you will need to have configured your AWS credentials.

Related Posts


Remove All Files In A Bucket

aws s3 rm --recursive s3://my-bucket

One cannot use the * character as a wildcard. S3 considers this a valid character for a filename. Hence the use of the --recursive flag.

Copy A Bucket To Another Bucket

aws s3 cp --recursive \
  s3://my-bucket \

One can use this to copy files between two different accounts, as long as one of the account's buckets is publicly accessible! The files go direct from server to server (e.g. not through you) so this is super fast!

Move A Bucket's Contents

Same as before but with "mv" instead of "cp"

Upload To Bucket

aws s3 cp \
  /path/to/local/file.txt \

Download File From Bucket

aws s3 cp \
  s3://my-bucket/sub-folder/file.txt \

Syncing Two Buckets

You can sync from one bucket to another with:

aws s3 sync \
   s3://BucketFrom \

If the buckets are across two different accounts, you will need one of the two buckets to be public, and have your credentials configured for the private one.

Get The Size Of An S3 Bucket

aws s3 ls \
  --summarize \
  --human-readable \
  --recursive \

Get The Size Of An S3 Bucket Folder

aws s3 ls \
  --summarize \
  --human-readable \
  --recursive \

Get Size Of S3 Bucket (Faster)

The above commands get the size of a bucket by looping through every file which takes a long time if you have a large bucket.

Instead, do the following:

sudo apt-get install s4cmd
s4cmd du --recursive s3://my-bucket-name

You will need to sum up the integers and plug in "total x 512 bytes" into Google to get a human readable number.

Last updated: 7th September 2023
First published: 24th March 2020