Bash globstar matching

bashglobstarwildcards

I have this directory structure:

~/tmp/globstar ɀ  find dir -type f
dir/file.ext
dir/subdir1/file.ext
dir/subdir2/file.ext

and, with the globstar option enabled in Bash, I can say:

~/tmp/globstar ɀ  ls -1 dir/**/*.ext
dir/subdir1/file.ext
dir/subdir2/file.ext

My question is: why is dir/file.ext excluded from this list?

Bash manual says this about globstar:

If set, the pattern ‘**’ used in a filename expansion context will match all files and zero or more directories and subdirectories. If the pattern is followed by a ‘/’, only directories and subdirectories match.

zero” in this paragraph let me with the impression that dir/file.ext should have been included; unless I’m hopefully missing something.

Best Answer

I guess that refers to the subdirectory level only. ** without / matches

  1. all files and directories

  2. zero or more subdirectories

But it does not completely disappear. **/ means that no files in the highest-level directory which ** applies to are matched.

You need dir/*.ext dir/**/*.ext.

Related Question