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,380
  • 25
  • 108
  • 167
  • 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](http://beyondgrep.com/) 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.

Dennis Kaarsemaker
  • 8,420
  • 3
  • 29
  • 31
11

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.)

  • In that case, you'd rather use `grep -l function_name /lib/lib*` or `nm -Do /lib/lib* | grep function_name` or `ldconfig -p | grep -o '/.*/lib.*' | xargs nm -Do | grep function_name` though. – Stéphane Chazelas Jun 09 '15 at 12:11
  • Well, `grep some_function /lib/*` is shorter and usually gives correct answer - name of library I am looking for. But yes, sometimes more sophisticated approach like your ones is required too. – Daniel Frużyński Jun 09 '15 at 12:42
2

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

grep --binary-files=text "data" f1 f2
chaos
  • 47,463
  • 11
  • 118
  • 144
user465055
  • 31
  • 1
  • This doesn't answer the question of _why_ the behaviour is like it is, nor is even useful in the context where one says they "cannot think of a scenario where I would care about matches in binary files". Note that the opposite, ignoring binary files, which is actually useful in that context was already mentioned in the question. – ilkkachu Apr 04 '21 at 16:25