Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How can I count the number of files (in a directory) containing a given string as input in bash/sh?

share|improve this question

4 Answers 4

up vote 11 down vote accepted

grep -l "string" * | wc -l will search for "given string" in the contents of all files in the working directory and tell you how many matched.

share|improve this answer
    
That will count the number of instances of the string. grep -l "string" * | wc -l will count the number of files. –  bahamat Jan 29 '11 at 21:18
    
@bahamat thanks it works ... –  user4311 Jan 29 '11 at 21:22
    
@bahamat: Thanks, I forgot grep had that option. –  Wolf Jan 29 '11 at 23:00
    
This won't work if there are a lot of files in the directory, it will throw the error "zsh: argument list too long: grep". Any ideas on how to get rid of this? –  user16142 Mar 2 '12 at 6:17

greps parameter -l will only output the filenames which are matching $PATTERN, wc can count them afterwards.

grep -l "$PATTERN" * | wc -l
share|improve this answer
    
thanks, but was wondering with a pattern in the file ... sorry for the ambiguous question –  user4311 Jan 29 '11 at 21:03
    
If so, take Wolf's answer. –  wag Jan 29 '11 at 21:04
awk '/pattern_to_look_for/ {s+=1; nextfile;} END {print s}' *

Clarification: This looks for the number of files that has the "pattern_to_look_for" in their contents and not in their filenames (like Wag's answer). From your question it's hard to tell what you are looking for.

share|improve this answer
    
But something is missing? because it does not work –  user4311 Jan 29 '11 at 21:01
    
If you look for the string duck you must write it like /duck/ in Awk. Are you doing that? –  Jan Persson Jan 29 '11 at 21:05
    
yes, the output: awk: read error (Is a directory) –  user4311 Jan 29 '11 at 21:07
    
Wolf's answer will give you duplicates. If there are several occurances of the string you are looking for in one of the files, it will count as extra matches. –  Jan Persson Jan 29 '11 at 21:08
    
Well, if you have directories in the folder you have to do some extra stuff like replacing the star with something like this find . ! -name . -prune -type f (don't miss the backticks). This command only lists the files in the folder. –  Jan Persson Jan 29 '11 at 21:11

This works in Bash with any filename:

grep -lZ "$pattern" * | tr -cd '\000' | wc -c

Explanation:

  • grep's -Z option prints every result with a ␀-byte separator. This character can't be part of a file name, so we can simply count the number of separators to get the number of files.
  • To get rid of the other characters in the output, we simply remove everything except the ␀ bytes using tr.
  • Then just count the characters with wc.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.