111

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?

4
  • 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"`. – Erik 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 2 '16 at 9: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 6:12
102

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.

3
  • 4
    The same goes for PDF files as well. – Joseph R. Mar 28 '13 at 17:45
  • 14
    + historically grep could NOT do that, so for compatibility only, default have to be all-inclusive. – Olivier Dulac Mar 28 '13 at 18:54
  • 2
    @OlivierDulac it makes sense, otherwise ignoring binary files should be the default. – Dagang Nov 25 '13 at 9:46
9

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

2
  • 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 9 '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 9 '15 at 12:42
0

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

grep --binary-files=text "data" f1 f2
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 4 at 16:25

Your Answer

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