Using regex in find command for multiple file types

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.

This should work

Messy

find . -iregex '.*\.\(c\|cc\|h\)' -exec grep -nHr "$1" {} +

-iregex for case-insensitive regex pattern.

(c|cc|h) (nasty escapes not shown) matches c, cc, or h extensions


Clean

find -regextype "posix-extended" -iregex '.*\.(c|cc|h)' -exec grep -nHr "$1" {} +

This will find .Cc and .cC extensions too. You have been warned.


This command works.

find -regextype posix-extended -regex '.+\.(h|H|c{1,2}|C{1,2})$'

I wish I could use iregex. iregex would also find .Cc and .cC. If I could, the command would look like this. Just a bit shorter.

find -regextype posix-extended -iregex '.+\.(h|H|c{1,2})$'