2012-12-08 16 views
9

我想查找所有以.c.R.Rd結尾的文件。我試圖Unix find:如何匹配文件* .R和* Rd?

find . -iname "*.[cR]" -print 

這給出了與.c.R結尾的所有文件。我怎樣才能另外獲得.Rd文件?我知道,只有[]匹配一個字符,但我無法調整它提供.Rd文件以及(試圖與|或選項-regex等工作)

回答

15

在這裏你去=)

find -name "*.c" -o -name "*.R" -o -name "*.Rd" 

如果它只是您正在尋找的3種擴展類型,我建議遠離正則表達式,並使用-o(如「或」)運算符來組合搜索。

+0

這個解決方案似乎取決於平臺:我在Windows PC上安裝了一個Cygwin UNIX模擬器,並且下面的find命令似乎正在工作:'find ./ -name「* .exe」'和'find ./ -name「* .dll」'但是'find ./ -name「* .exe」-o name「* .dll」'似乎不起作用。 – Dominique

7

合適的使用-regex是:

find -regex '.*\.\(R\|Rd\|c\)' 

如果你想使用,你需要記住的是,這些適用於整個路徑,而不是隻對文件名正則表達式:

-regex pattern 
      File name matches regular expression pattern. This is a match 
      on the whole path, not a search. For example, to match a file 
      named `./fubar3', you can use the regular expression `.*bar.' or 
      `.*b.*3', but not `f.*r3'. The regular expressions understood 
      by find are by default Emacs Regular Expressions, but this can 
      be changed with the -regextype option. 

我同意sampson-chen的回答,即正則表達式可能不是這裏最好的選擇。