Solved

Find files that contain a string, and display line counts

Posted on 2014-12-18
3
245 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
ID: 40508495
find . -type f -name "P*" | xargs grep -c foo

or

grep -rc P*
0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 40508498
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
ID: 40508626
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
bash script question (chmod) 10 53
a free alternative to cpanel? 1 58
Xymon customize http timeout 2 61
AWS CLI issues with Tags 3 62
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

947 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

23 Experts available now in Live!

Get 1:1 Help Now