1

I have done this by mistake:

s3cmd del s3://mybucket/*

But ... it is working:

...
delete: 's3://mybucket/file0080.bin'
delete: 's3://mybucket/file0081.bin'
delete: 's3://mybucket/file0082.bin'
...

I am baffled. Usually * is expanded by the shell (Bash), using the information available in the localhost.

How/why is expansion working against an s3 bucket?

(This is an unquoted glob pattern)

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
blueFast
  • 41,341
  • 63
  • 198
  • 344
  • `scp` behaves much the same, applying globs remotely after invocation; you'll note that `scp host:*.txt .` also works (and indeed, works more reliably) as `scp 'host:*.txt' .` – Charles Duffy May 23 '18 at 12:35

1 Answers1

3

If the glob doesn’t match anything it’ll remain as-is (unless you set the nullglob option in Bash), with an asterisk in this case, and s3cmd del apparently understands that.

Of course it’s not a good idea to rely on this behaviour, since if a local file should suddenly exist that matches the glob it would (probably) stop working. Quoting the glob (i.e. making it not a glob) is a good habit.

An other option is to set the nullglob option (shopt -s nullglob) to make non-matching globs go away entirely.

To see how a glob expands and what the final command looks like you can run set -x in Bash before running it, which makes Bash print each (expanded) command before running it (set +x to turn it off).

Biffen
  • 6,249
  • 6
  • 28
  • 36
  • 1
    So this *feature* will work only whenever the pattern is not expanded locally. It very much depends on you not having anything matching the pattern locally then (which is somehow safe, since the pattern starts with `s3://mybucket/`). A bit brittle. Probably better to be safe and do `s3cmd del 's3://mybucket/*'`? – blueFast May 23 '18 at 12:18
  • @dangonfast Yes and yes. I wouldn’t recommend relying on the glob not being expanded. – Biffen May 23 '18 at 12:20
  • Could you add that suggestion to you answer? Good for me to accept. – blueFast May 23 '18 at 12:24
  • 1
    BTW, `nullglob` isn't the only option that will modify this behavior -- consider `failglob`, causing the command to not execute (and `s3cmd` to not be invoked) in the zero-match case. – Charles Duffy May 23 '18 at 12:37