Unix查找:如何匹配文件*.R和*Rd?

Mar*_*ert 11 regex find

我想找到与结尾的所有文件.c,.R.Rd.我试过了

find . -iname "*.[cR]" -print
Run Code Online (Sandbox Code Playgroud)

它给出所有以.c或结尾的文件.R.我怎么能另外获取.Rd文件?我知道[]只匹配一个字符,但我无法调整它以提供.Rd文件(尝试使用|或选项-regex等)

sam*_*hen 17

你去=)

find -name "*.c" -o -name "*.R" -o -name "*.Rd"
Run Code Online (Sandbox Code Playgroud)

如果它只是您要查找的3种扩展类型,我建议远离正则表达式,只需使用-o(如"或")运算符来编写搜索.


Lar*_*ski 8

适当的用途-regex是:

find -regex '.*\.\(R\|Rd\|c\)'
Run Code Online (Sandbox Code Playgroud)

如果你想使用正则表达式,你需要记住,这些适用于整个路径,而不仅仅是文件名:

   -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.
Run Code Online (Sandbox Code Playgroud)

我同意sampson-chen的回答,正则表达式可能不是这里的最佳选择.