AWS CLI S3 Cheatsheet - s3 ls

AWS CLI S3 Cheatsheet - s3 ls

The ls command can be used for listing S3 buckets or objects inside a bucket. When used to list objects, the command will show you only objects in the current prefix.

List all buckets

~ aws s3 ls
2019-12-15 16:31:53 testbucket1
2020-03-11 14:44:32 testbucket112312031230
2020-06-01 09:06:26 myPersonalTestBucket

List objects inside a bucket

~ aws s3 ls s3://myPersonalTestBucket/
                           PRE 2020/
2019-12-15 18:05:39      69483 test.PNG

When ls it's used inside a bucket, it will return all objects and prefixes inside the current level. You can navigate prefixes as a sort of virtual directory.
In this case, we can also navigate down inside the 2020 prefix:

~ aws s3 ls s3://myPersonalTestBucket/2020/09/01/20/20/
2019-12-15 18:23:46   32864363 complete-logs.txt
2019-12-15 18:23:46     463004 light-logs.pdf

Path matching

Let's take into consideration a bucket with content similar to this one:

~ aws s3 ls s3://testbucket1
                           PRE ABC/
                           PRE ACC/
                           PRE ACZ/
                           PRE AZC/
                           PRE BCA/
                           PRE BCZ/

What if we want to list all prefixes that start with A or AC:

~ aws s3 ls s3://testbucket1/A
                           PRE ABC/
                           PRE ACC/
                           PRE ACZ/
                           PRE AZC/
~ aws s3 ls s3://testbucket1/AC
                           PRE ACC/
                           PRE ACZ/

And what if we want to list all objects starting with a prefix:

~ aws s3 ls s3://testbucket1/AC --recursive
2019-12-15 18:22:17     99731 ACC/test1.pdf
2019-12-15 18:22:17     1916928 ACZ/test2.pdf
2019-12-15 18:22:17     112989 ACZ/test3.pdf

Human option

The human option helps read the filesize.

~ aws s3 ls s3://testbucket1/AC --recursive --human
2019-12-15 18:22:17     97.4 KiB ACC/test1.pdf
2019-12-15 18:22:17     1.8 MiB ACZ/test2.pdf
2019-12-15 18:22:17     110.3 KiB ACZ/test3.pdf

Summarize option

Summarize option add two rows at the end of the output with a short recap:

  • total objects listed

  • the total size of objects listed

You can use --summarize in combination with --human to have the total size of objects listed a little more readable.

~ aws s3 ls s3://testbucket1/AC --recursive --human --summarize
2019-12-15 18:22:17     97.4 KiB ACC/test1.pdf
2019-12-15 18:22:17     1.8 MiB ACZ/test2.pdf
2019-12-15 18:22:17     110.3 KiB ACZ/test3.pdf
Total Objects: 3
   Total Size: 2.1 MiB