How to return both file name and line number with find ... -exec grep?

The command line switch -H forces grep to print the file name, even with just one file.

% grep -n 7 test.in
7:7
% grep -Hn 7 test.in
test.in:7:7

   -H, --with-filename
          Print the filename for each match.

Note that as Kojiro says in a comment, this is not part of the POSIX standard; it is in both GNU and BSD grep, but it's possible some systems don't have it (e.g. Solaris).


find . -type f -exec grep -n 'string to search' /dev/null {} +

(or with GNU grep, see the -H option)


If your grep supports the recursive -r flag, this solves your request:

grep -rn "String to search " * 

Tags:

Grep

Find