118

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?

Braiam
  • 35,991
  • 4
    You can set the variable GREP_OPTIONS to your preferred settings, then you don't have to use that many command line switches. – Marco Mar 28 '13 at 16:02
  • 2
    A note for other commands that do not support such a variable: You can set default options by defining an alias in your .(ba|z|foo)shrc':alias grep="grep -I"`. – Nova Mar 28 '13 at 19:19
  • 1
    This can actually be very useful. For example, I was searching for the unity version of a project, and couldn't remember which file it was kept in. I searched for the format of the version, and it came up with some binary files that matches. It turned out as it was pre-version 5, the version was only present in a binary file, so without the binary match, I might have missed it. – Svend Hansen Feb 02 '16 at 09:17
  • FYI There's a tool ack which is kind of "better than grep". Ack doesn't search binary files. In Debian, it comes under the name "ack-grep". – teika kazura Feb 16 '17 at 06:12

3 Answers3

107

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.

  • 2
    @OlivierDulac it makes sense, otherwise ignoring binary files should be the default. – Dagang Nov 25 '13 at 09:46