Find text or string in files of a certain type

Linux
Mac OS X
Author

Vinh Nguyen

Published

April 11, 2013

One can use grep "mystring" myfile.ext to find the lines in myfile.ext containing mystring. One could also use grep "mystring" *.ext to find mystring in all files with extension ext. Similarly, one could use grep "mystring" /directory to search for mystring in all files in the directory. What if one wants to search for mystring in all *.ext files in a certain path /directory? Most posts online would suggest something along the line of

find /directory -type -f -name "*.ext" | xargs grep "mystring"

However, the comments of this post shows how one could do it with grep:

grep -r --include=*.ext "mystring" /directory