How to get no. of lines count that matches a string from all the files in a folder How to get no. of lines count that matches a string from all the files in a folder linux linux

How to get no. of lines count that matches a string from all the files in a folder


You can pipe your grep with wc -l to get count of lines containing your keyword:

grep -r "string_example" . | wc -l


You could also use awk to do this:

awk '/string_example/{++c}END{print c}' *

c is incremented every time a line matches the pattern. Once all files have been read, print the total count.


You want something like this?

grep -l string_example *|xargs wc -l

Edit:
You want to get numer of lines that matched in all files, or total numer of lines in files that contains matched line?