2010-08-20 121 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等等。

  • 其他連續的星號被認爲是無效的。