Find files that contain a string, and display line counts

I use below to list all P* files that contain "foo"

find . -type f -name "P*" -exec grep -l foo {} \;

How should I change this command to also display line count next to each file name?
Who is Participating?
TintinConnect With a Mentor Commented:
In case, I misunderstood your question and you want the total line count in the file, do

grep -rl foo P* | xargs wc -l
find . -type f -name "P*" | xargs grep -c foo


grep -rc P*
find basedir1 basedir2 -type f -name P* -print0 | xargs -0 wc -l
(this one starts one wc command for many arguments, a bit more efficient than re-executing for each file)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.