Using regex in find command for multiple file types

bash, file-extension, regex

Solution

You can use the boolean OR argument:

find . -name '*.[ch]' -o -name '*.[CH]' -o -name '*.cc' -o -name '*.CC'

The above searches the current directory and all sub-directories for files that end in:

- `.c`, `.h` OR

- `.C`, `.H` OR

- `.cc` OR

- `.CC`.

Problem

I am currently using ``` find . -name '*.[cCHh][cC]' -exec grep -nHr "$1" {} ; \ find . -name '*.[cCHh]' -exec grep -nHr "$1" {} ; ``` to search for a string in all files ending with .c, .C, .h, .H, .cc and .CC listed in all subdirectories. But since this includes two commands this feels inefficient. How can I use a single regex pattern to find .c,.C,.h,.H,.cc and .CC files? I am using bash on a Linux machine.

Original source

Related problems