11

I would like to find all files that end with .c, .R, or .Rd. I tried

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

which gives all files that end with .c or .R. How can I additionally get .Rd files as well? I know that [] only matches one character, but I couldn't adjust it to provide .Rd files as well (tried to work with | or option -regex etc.)

jww
  • 97,681
  • 90
  • 411
  • 885
Marius Hofert
  • 6,546
  • 10
  • 48
  • 102

2 Answers2

18

Here you go =)

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

If it's just the 3 extension types that you are looking for, I'd recommend staying away from regex and just using the -o (as in "or") operator to compose your search instead.

sampson-chen
  • 45,805
  • 12
  • 84
  • 81
  • This solutions seems to be platform dependent: I have installed a Cygwin UNIX emulator on my Windows PC, and the following find command seem to be working: `find ./ -name "*.exe"` and `find ./ -name "*.dll"` but `find ./ -name "*.exe" -o name "*.dll"` seems not to work. – Dominique Aug 09 '17 at 09:20
8

A suitable use of -regex would be:

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

If you want to use regular expressions you need to keep in mind that these apply to the whole path, not only to the filename:

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

I agree with sampson-chen's answer, that regexes are probably not the best choice here.

Lars Noschinski
  • 3,667
  • 16
  • 29