Number of files containing a given string

Viewed 45

I want to show text files that contain a string and to count them. My code is:

grep -l "string" file1.txt file2.txt | wc -l

It shows the number of files that contain "string". How can I show the names of the files too?

1 Answers

How about teeing it?

grep -l 'string' file1.txt file2.txt | tee >(wc -l)
Related