How does grep run so fast?

Assuming your question regards GNU grep specifically. Here’s a note from the author, Mike Haertel: GNU grep is fast because it AVOIDS LOOKING AT EVERY INPUT BYTE. GNU grep is fast because it EXECUTES VERY FEW INSTRUCTIONS FOR EACH BYTE that it does look at. GNU grep uses the well-known Boyer-Moore algorithm, which looks first … Read more

Use grep –exclude/–include syntax to not grep through certain files

Use the shell globbing syntax: grep pattern -r –include=\*.cpp –include=\*.h rootdir The syntax for –exclude is identical. Note that the star is escaped with a backslash to prevent it from being expanded by the shell (quoting it, such as –include=”*.cpp”, would work just as well). Otherwise, if you had any files in the current working … Read more