Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

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?
0
rudy201
Asked:
rudy201
  • 2
1 Solution
 
TintinCommented:
find . -type f -name "P*" | xargs grep -c foo

or

grep -rc P*
0
 
TintinCommented:
In case, I misunderstood your question and you want the total line count in the file, do

grep -rl foo P* | xargs wc -l
0
 
gheistCommented:
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)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now