How can I grep in source files for some text?

ack (or, on Debian/Ubuntu, ack-grep) will ignore non-source files like version control or binaries. Very useful.

to search just .c and .h files, as above:

ack-grep -i --cc "invalid preset"

the --cc (the longer form is --type cc) only looks at .c .h & .xs files. The full list of filetypes is viewable with ack-grep --help type. Most of the time, you won't particularly need the --type, as it will generally only have the files to search, and then files you won't see by default, like binaries, backups and version control files.


The grep program itself can search recursively and also accepts an option to search only certain files. The following is equivalent to your two find commands.

grep -Ri --include=*.[ch] invalid\ preset .