46

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.

Nicros
  • 5,031
  • 12
  • 57
  • 101
  • Very inefficient, but why not do something like "aws s3 ls s3://my-bucket/folder/ | grep myfile" ? That is, if you insist on using the CLI. A python script using Boto would be much more efficient, though it's not a single line command. – Gil Adirim Oct 04 '16 at 17:17
  • @GilAdirim Actually that would work as well. I was just surprised that the aws-cli didn't provide something like this and thought I was missing something – Nicros Oct 04 '16 at 17:41
  • @GilAdirim I've been looking at Boto and I don't see how it supports wildcards either, do you have a pointer you could provide? – Ken Williams Dec 15 '16 at 19:30

5 Answers5

65

aws s3 ls does not support globs, but sync does and has a dry run mode. So if you do this (from an empty directory) you should get the results you want:

aws s3 sync s3://my-bucket . --exclude "*" --include "folder/*myfile*" --dryrun

It will produce lines like this for matching files:

(dryrun) download s3://my-bucket/folder/myfile.txt to folder/myfile.txt
(dryrun) download s3://my-bucket/folder/_myfile-foo.xml to folder/_myfile-foo.xml
Chrs
  • 862
  • 6
  • 10
33

(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)

Gil Adirim
  • 1,834
  • 2
  • 21
  • 33
14

S3 doesn't support wildcard listing. You need to list all the files and grep it.

aws s3 ls s3://mybucket/folder --recursive 

Above command will give the list of files under your folder, it searches the files inside the folder as well. Just grep your file name

aws s3 ls s3://mybucket/folder --recursive |grep filename

Suppose if you want to find multiple files, create a regular expression of those and grep it.

skipper21
  • 2,165
  • 2
  • 16
  • 13
  • 3
    This will produce inaccurate results if you have more than 1000 objects in the bucket. – Dave Yarwood Apr 29 '18 at 00:00
  • 1
    @DaveYarwood: could you specify why? I know these operations are not nearly as reliable as analogous filesystem ones, but a quick search didn't turn up much for me. – user1071847 Dec 05 '18 at 14:32
  • IIRC, there is a limit of 1000 on the number of items that `aws s3 ls` will return. – Dave Yarwood Dec 05 '18 at 19:12
  • 3
    @DaveYarwood, do you mean there is a *page-size* limit of 1000, because `aws s3 ls` will clearly return more than 1000 values (as I verified just now). I understand the docs to mean that the page limit is **per API call** happening behind the scenes. – Arel Mar 18 '19 at 17:40
3

For Windows User:

aws s3 ls s3://my-bucket/folder/ | findstr myfile
1

2022 Update:

You can use this supported with aws s3 with the --dry-run option:

aws s3 sync s3://my-bucket . --include "folder/*myfile*" --exclude "*" --dryrun

Alternatively,

s3cmd also works for me with grep.

s3cmd ls --recursive s3://mybucket/folder/folder2 | grep filename
Rico
  • 58,485
  • 12
  • 111
  • 141