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?
-
2@skwllsp But with -l, the results do not contain the matched line, only with matched file name.RandyTek– RandyTek2014-09-15 17:44:23 +00:00Commented Sep 15, 2014 at 17:44
-
5The reason for closing this question could have been more clear. It is a proper question for Unix & Linux IMO. Probably, the reason could explain that this question is more suited for another Stack Exchange site.Sadman Sakib– Sadman Sakib2022-06-29 03:47:12 +00:00Commented Jun 29, 2022 at 3:47
-
3Well, grep is a "software tools primarily used by programmers.", so I don't understand why the close vote; at least it is answeredsdbbs– sdbbs2025-03-11 17:56:09 +00:00Commented Mar 11 at 17:56
Add a comment
|
2 Answers
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 *
6 Comments
vladkras
I'd use
-Irn where r stands for recursive to look inside all folders. H is exsessive herecp.engr
@vladkras, "H is exsessive here" - you mean redundant, i.e. it's already the default?
jmrah
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.
jvriesem
@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.Toby
n and H are not required. The only option necessary to answser this question in full is -I
|
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$
4 Comments
slow-but-steady
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!
pjay
@amaidain In what situations do you need to treat a binary as a text file? I havent used it, can you please elaborate.
agrajag_42
@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.
amadain
@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