2010-08-20 44 views
21

在Ryan Bates的Railscast about git,他的.gitignore文件包含以下行:Unix通配符选择器? (星号)

tmp/**/*

什么是使用双星号后跟一个星号这样的目的:**/*? 会使用简单的tmp/*而不是tmp/**/*达不到完全相同的结果?

使用谷歌搜索这个问题,我发现了一篇关于它的不清楚的IBM文章,我想知道是否有人能够澄清这个问题。

+0

注:虽然有些shell支持这种语法,但Git不支持。在'.gitignore'文件中,这相当于'tmp/*/*'。 – hammar 2012-10-02 07:09:02

回答

24

它说要进入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 
+1

很好的解释。谢谢! – 2010-08-27 16:18:32

+0

在Unix中是否有这种模式匹配的命名法?我试图找到更多信息,但我不知道如何Google。 – Jondlm 2013-11-29 16:00:32

+0

文件的模式匹配称为globbing。基本变体是用于0个或更多字符的'*',用于任何字符的'''和用于匹配特定范围的字符的[[CharacterRange]],例如, '[0-9]'匹配一个数字。一些shell以自己的方式扩展它,其中包括'**'语法。 – Petesh 2013-11-29 17:43:00

5

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." 
1

根据the documentation of gitignore,这句法因为Git版本1.8.2支持。

下面是相关部分:

两个连续的星号(**)的模式对全路径名匹配可能有特殊的含义:

  • 领先**跟一个斜线意味着比赛所有目录。例如,**/foo与任何地方的文件或目录foo匹配, 与模式foo相同。 **/foo/bar与直接在目录foo下的任何地方的文件或目录bar 匹配。

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

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

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