3

Find all files recursively with specified string in the filename and output any lines found containing a different string

find . -name '*conf*' -exec grep -Hni 'text to match' {} \; > matches.txt

April 14, 2014n00tz

Explanation

find . -type f -name '*conf*' -- find in the current directory (.) and in all subdirectories recursively all the files with conf in the filename.

-exec grep -Hni 'text to match' {} \; -- for each file, execute the grep command to find all lines within the file containing text to match. The text to match can be a regular expression.

The flags of grep:

  • -i ignore case; for example Foo, FOO, foo would all match the pattern foo.
  • -H prefix the output lines (the matched lines) with the filename
  • -n prefix the output lines with the liner number

Notice the \; at the end of the find command, this is required to terminate the -exec command.

Finally with the output redirection > matches.txt we save the results to a file.