Solved

Find files that contain a string, and display line counts

Posted on 2014-12-18
3
244 Views
Last Modified: 2015-01-16
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
Comment
Question by:rudy201
  • 2
3 Comments
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
find . -type f -name "P*" | xargs grep -c foo

or

grep -rc P*
0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
Comment Utility
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
 
LVL 61

Expert Comment

by:gheist
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now