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' *
Output
validate_data_stage1:0
validate_data_stage2:0
validate_data_stage3:0
validate_data_stage4:0
validate_data_stage5:0
validate_input_stage1:0
validate_input_stage2:0
validate_input_stage3:0
validate_input_stage4:0
.... 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?
Best Answer
That's the behavior exhibited by
grep -c
.Probably you have a file whose name starts with
-
and contains ac
character and you're using GNU grep without setting thePOSIXLY_CORRECT
environment variable.Use:
or better:
--
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-optiondelete
argument would have marked the end of options), but it wouldn't address the problem of a file called-
. Thegrep 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).