user3457456 user3457456 - 1 year ago 87
Bash Question

Using find on multiple file extensions in combination with grep

I am having a problems using find and grep together in msys on Windows. However, I also tried the same command on a Linux machine and it behaved the same. Notwithstanding, the syntax below is for windows in that the semicolon on the end of the command is not preceded by a backslash.

I am trying to write a find expression to find *.cpp and *.h files and pass the results to grep. If I run this alone, it successfully finds all the .cpp and .h files:

find . -name '*.cpp' -o -name '*.h'

But if I add in an exec grep expression like this:

find . -name '*.cpp' -o -name '*.h' -exec grep -l 'std::deque' {} ;

It only greps the .h files. If I switch the .h and .cpp order in the command, it only searches the .h. Essentially, it appears to only grep the last file extension in the expression. What do I need to do to grep both .h and .cpp??

Answer Source

Since you're using -o, you will need to use parentheses around it:

find . \( -name '*.cpp' -o -name '*.h' \) -exec grep -l 'std::deque' {} \;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download