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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With