Check if file exists in s3 using ls and wildcard

Nicros picture Nicros · Oct 4, 2016 · Viewed 27.3k times · Source

Seems so simple, but not getting the syntax right. I want to know if a file exists in my s3 bucket using wildcards. Something like

aws s3 ls s3://my-bucket/folder/*myfile*

The goal is to see if a file called 2016_myfile.txt or a file called 2011_myfile.csv exists within this bucket.

If I run the command, it doesn't return anything even though I know this file exists there.

Answer

Gil Adirim picture Gil Adirim · Oct 4, 2016

(re-drafted from comment as it appears this answered the question)

I myself tried, and failed to use wildcards in the aws-cli, and according to the docs, this is not currently supported. Simplest (though least efficient) solution would be to use grep:

aws s3 ls s3://my-bucket/folder/ | grep myfile

Alternatively, you could write a short python/other script to do this more efficiently (but not in a single command)