Unix通配符select器? (星号)

在Ryan Bates 关于git的Railscast中 ,他的.gitignore文件包含以下行:

tmp/**/*

使用星号后加星号的目的是什么: **/* ? 将使用简单的tmp/*而不是tmp/**/*不能达到完全相同的结果?

使用Googlesearch这个问题,我发现了一篇不太明确的IBM文章,我想知道是否有人能够澄清这个问题。

它说要进入tmp下面的所有子目录,以及tmp的内容。

例如我有以下几点:

 $ find tmp tmp tmp/a tmp/a/b tmp/a/b/file1 tmp/b tmp/b/c tmp/b/c/file2 

匹配输出:

 $ echo tmp/* tmp/a tmp/b 

匹配输出:

 $ echo tmp/**/* tmp/a tmp/a/b tmp/a/b/file1 tmp/b tmp/b/c tmp/b/c/file2 

这是zsh的默认功能,让它在bash 4中工作,你执行:

 shopt -s globstar 

来自http://blog.privateergroup.com/2010/03/gitignore-file-for-android-development/

(kwoods)

 "The double asterisk (**) is not a git thing per say, it's really a linux / Mac shell thing. It would match on everything including any sub folders that had been created. You can see the effect in the shell like so: # ls ./tmp/* = should show you the contents of ./tmp (files and folders) # ls ./tmp/** = same as above, but it would also go into each sub-folder and show the contents there as well." 

根据gitignore的文档 ,这个语法从git版本1.8.2开始支持。

这里是相关的部分:

与完整路径名相匹配的连续两个星号( ** )可能有特殊含义:

  • 前面的**后跟斜杠表示所有目录匹配。 例如, **/foo匹配任何地方的文件或目录foo ,与pattern foo相同。 **/foo/bar与直接在目录foo下的任何地方的文件或目录**/foo/bar相匹配。

  • 尾随/**匹配里面的所有内容。 例如, abc/**匹配目录abc中的所有文件,相对于.gitignore文件的位置,具有无限深度。

  • 斜杠后跟两个连续的星号,则斜线匹配零个或多个目录。 例如, a/**/b匹配a/ba/x/ba/x/y/b等等。

  • 其他连续的星号被认为是无效的。