how to find files containing a string using egrep

find, grep, linux

Solution

Here you are sending the file names (output of the `find command`) as input to egrep; you actually want to run egrep on the contents of the files.

Here are a couple of alternatives:

find . -name "*.txt" -exec egrep mystring {} \;

or even better

find . -name "*.txt" -print0 | xargs -0 egrep mystring

Check the find command help to check what the single arguments do. The first approach will spawn a new process for every file, while the second will pass more than one file as argument to egrep; the -print0 and -0 flags are needed to deal with potentially nasty file names (allowing to separate file names correctly even if a file name contains a space, for example).

Problem

I would like to find the files containing specific string under linux. I tried something like but could not succeed: find . -name *.txt | egrep mystring

Original source