Solved

Find files that contain a string, and display line counts

Posted on 2014-12-18
3
247 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 62

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Fine Tune your automatic Updates for Ubuntu / Debian
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

809 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