Aish Mahesh
Aish Mahesh

Reputation: 231

AWS CLI Commands

I want to get list of all files in S3 bucket with particular naming pattern. For Eg if i have files like

aaaa2018-05-01
aaaa2018-05-23
aaaa2018-06-30
aaaa2018-06-21

I need to get list of all files for 5th month.Output should look like:

aaaa2018-05-01
aaaa2018-05-23

I executed the following command and the result was empty:

aws s3api list-objects --bucket bucketname --query "Contents[?contains(Key, 'aaaa2018-05-*')]" > s3list05.txt

when i check the s3list05.txt its empty. Also i tried the below command and

aws s3 ls s3:bucketname --recursive | grep aaaa2018-05* > s3list05.txt

this command lists me all the objects present in the file. Kindly let me know the exact command to get desired output.

Upvotes: 1

Views: 1199

Answers (2)

John Rotenstein
John Rotenstein

Reputation: 270089

The Contains parameter doesn't need a wildcard:

aws s3api list-objects --bucket bucketname --query "Contents[?contains(Key, 'aaaa2018-05')].[Key]" --output text

This provides a list of Keys.

--output text removes the JSON formatting.

Using [Key] instead of just Key puts them all on one line.

Upvotes: 1

helloV
helloV

Reputation: 52433

You are almost there. Try this:

aws s3 ls s3://bucketname --recursive | grep aaaa2018-05

or

aws s3 ls bucketname --recursive | grep aaaa2018-05

Upvotes: 1

Related Questions