Find text or string in files of a certain type

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

<pre class="src src-sh">find /directory -type -f -name <span style="color: #ffa07a;">"*.ext"</span> | xargs grep <span style="color: #ffa07a;">"mystring"</span>

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

<pre class="src src-sh">grep -r --include=*.ext <span style="color: #ffa07a;">"mystring"</span> /directory

Find files and find files containing certain text

This is a reminder to myself as I keep forgetting how to do these basic searches in Linux.

To find files with file name containing the text foo using the command find:

## find file with "foo" in file name
find ./ -name "*foo*" ## replace ./ with path; can use shell style wildcards
## ignore upper and lower cases
find ./ -iname "*foo*"
## print pathnames of all files
find ./ -print

To find files with bar in it’s content using the command grep:

grep "bar" -r ./
## print lines without the word "bar"
grep -v "bar" -r ./
## note, can also use regexp with -E