Why doesn't grep ignore binary files by default?

The manpage for grep describes the -I flag as follows:

-I      Ignore binary files.  This option is equivalent to 
        --binary-file=without-match option.

It also says this about binary files:

 --binary-files=value Controls searching and printing of binary files.
         Options are binary, the default: search binary files but do not print
         them; without-match: do not search binary files; and text: treat all
         files as text.

I cannot think of a scenario where I would care about matches in binary files. If such a scenario exists, surely it must be the exception rather than the norm. Why doesn’t grep ignore binary files by default rather than requiring setting this flag to do so?

Asked By: user36078

||

Not everything that grep thinks is a binary file, is actually a binary file. e.g. puppet’s logs have ansi color coding in them, which makes grep think they’re binary. I’d still want to search them if I’m grepping through /var/log though.

Answered By: Dennis Kaarsemaker

grep’s ability to search binary files is also useful when I’m compiling a program and the linker (ld) complains about some function not being found. 
I can use a command like

grep function_name /lib/lib*

to find the library which contains it.  (Libraries are binary files.)

Answered By: Daniel Frużyński

To process a binary file or a file that grep thinks is a binary file, use

grep --binary-files=text "data" f1 f2
Answered By: user465055
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.