339

When using grep in linux, the result often contains a lot of "binary file XXX matches", which I do not care about. How to suppress this part of the results, or how to exclude binary files in grep?

Mefitico
  • 816
  • 1
  • 12
  • 41
RandyTek
  • 4,374
  • 3
  • 23
  • 32
  • 1
    @skwllsp But with -l, the results do not contain the matched line, only with matched file name. – RandyTek Sep 15 '14 at 17:44
  • 2
    The reason for closing this question could have been more clear. It is a proper question for [Unix & Linux](https://unix.stackexchange.com/) IMO. Probably, the reason could explain that this question is more suited for another Stack Exchange site. – Sadman Sakib Jun 29 '22 at 03:47
  • 2
    Closing this was silly. grep is definitely among the "software tools primarily used by programmers" Plus, look at all the tags! https://stackoverflow.com/questions/tagged/grep – Levi Baguley Dec 15 '22 at 22:55

2 Answers2

476

There are three options, that you can use. -I is to exclude binary files in grep. Other are for line numbers and file names.

grep -I -n -H 


-I -- process a binary file as if it did not contain matching data; 
-n -- prefix each line of output with the 1-based line number within its input file
-H -- print the file name for each match

So this might be a way to run grep:

grep -InH your-word *
  • 22
    I'd use `-Irn` where `r` stands for recursive to look inside all folders. `H` is exsessive here – vladkras Apr 19 '16 at 09:55
  • @vladkras, "H is exsessive here" - you mean redundant, i.e. it's already the default? – cp.engr Feb 21 '18 at 17:51
  • Thank you for clarifying what the short options mean in your answer. There are so many terse linux command answers on SO that give no explanation, which I find annoying. – jmrah Apr 11 '18 at 14:30
  • I'm confused what exactly does `-n` do? What is 1-based? – Aaron Franke Dec 30 '18 at 00:50
  • 4
    @AaronFranke: The `-n` flag tells grep to report the line numbers of files wherein it found a match. "1-based" means that the line counting starts from one rather than zero, as is often done in programming. So, if the first line of your file named `example.txt` is `Hello, world`, the second line is `Hello cat`, and the third line is `cats are cool`, then searching for "cat" via `grep -n cat example.txt`, you'd get `example.txt:2: Hello cat` and `example.txt:3: cats are cool`. – jvriesem Jan 09 '19 at 23:26
31

This is an old question and its been answered but I thought I'd put the --binary-files=text option here for anyone who wants to use it. The -I option ignores the binary file but if you want the grep to treat the binary file as a text file use --binary-files=text like so:

bash$ grep -i reset mediaLog*
Binary file mediaLog_dc1.txt matches
bash$ grep --binary-files=text -i reset mediaLog*
mediaLog_dc1.txt:2016-06-29 15:46:02,470 - Media [uploadChunk  ,315] - ERROR - ('Connection aborted.', error(104, 'Connection reset by peer'))
mediaLog_dc1.txt:ConnectionError: ('Connection aborted.', error(104, 'Connection reset by peer'))
bash$
amadain
  • 2,724
  • 4
  • 37
  • 58
  • 1
    This was super useful and works great when you don't want to ignore binary files! This works perfectly on files that are treated as binary but are files you still need to look through, whereas the accepted answer just ignores those files. Thanks! – slow-but-steady Nov 06 '20 at 04:02
  • @amaidain In what situations do you need to treat a binary as a text file? I havent used it, can you please elaborate. – pjay Apr 02 '21 at 15:56
  • 1
    @pjay One use case is when you want to read the binary logs of a database (MySQL in my case). I can decode the binary log and then I want to use grep to only keep the queries stored in the log. Using grep without --binary-files=text excludes pretty much all queries. – agrajag_42 May 07 '21 at 13:43
  • 2
    @pjay I found it occurring a lot when trying to get information from log files for various systems. Log files can have all sorts of information and when someone uses a emoji in their username the entire file can be marked as binary – amadain May 08 '21 at 16:23