I am trying to find files containing a specific word using grep. There are many files in the directory (> 500)

Command I run

$ grep 'delete' *


.... and hundred of such lines

These are the files that don't contain the given match. I want to suppress those lines from displaying to stdout. I know of -q switch, but that would suppress the complete output, which I don't want.

How do I do that?

  • 2
    Normally, grep should not print out file names of non-matching files. Actually, it looks like grep considers a line with the content 0 to be matching. Can you post the exact search pattern you are using? – Adrian Heine Nov 22 '12 at 9:31
  • The complete grep cmd I used was grep 'delete' * -R, but I don't think -R is causing any issue. And yes, it normally doesn't print the non-matching ones, but not sure what's the case here... – mtk Nov 22 '12 at 9:40
  • @step @everyone.. Strange... After seeing the edited post above. I tried grep -- 'delete' * (added --) and it worked as expected. Removing the -- is leading to the above display. – mtk Nov 22 '12 at 11:50
  • 1
    @Stephane Indeed you are correct. I searched the dir... and there was a file with name -ci. Thanks for resolving the issue. You can post this scenario as an answer. – mtk Nov 22 '12 at 13:29

That's the behavior exhibited by grep -c.

Probably you have a file whose name starts with - and contains a c character and you're using GNU grep without setting the POSIXLY_CORRECT environment variable.


grep -- delete *

or better:

grep delete ./*

-- marks the end of options so that that filename will not be considered as an option (with a POSIX grep, it wouldn't since the non-option delete argument would have marked the end of options), but it wouldn't address the problem of a file called -. The grep delete ./* is more robust but has the drawback of outputting the extra ./ for matching files (though that may be considered a bonus since that helps identify file names that contain newline characters).



| grep -v ':0$'

Not very elegant but I think will do the job.

Adding -l to your command will give you only matches but it will also suppress printing number of matches for each file.

  • 1
    -l worked. the work-around is also good. Thanks – mtk Nov 22 '12 at 11:47
  • 4
    the grep line should be grep -v ':0$' so only those at the end of a line are matched (however, the grep -l approach is far better as long as you don't need line counts) – mreithub Nov 22 '12 at 12:17

Using grep -l you will only get the files that contain at least one match.

Do you need information about how many matches there are in a file at all? Because the you could skip using -c which is used to count the number of matches in a file.

edit: And like warlock said using -I to suppress matches in binary files could also be a good idea.


How to grep nonzero counts:

grep -rIcH 'string' . | grep -v ':0$'

  • -r Recurse subdirectories.
  • -I Ignore binary files (thanks @tongpu, warlock).
  • -c Show count of matches. Annoyingly, includes 0-count files.
  • -H Show file name, even if only one file.
  • 'string' your string goes here.
  • . Start from the current directory.
  • | -v ':0$' Remove 0-count files. (thanks @LaurentiuRoescu)

(I realize the OP was unintentionally -c counting, but google(grep nonzero count) lands here.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.