241

I've got a grep script that searches through a directory recursively.

grep -n -R -e 'search term' -e 'second search term' ./ 

However the results I get are the following. Notice there are found matches in JPGs but no actual result.

Binary file ./jpg/00015928.jpg matches
Binary file ./jpg/00015296.jpg matches
Binary file ./jpg/00020072.jpg matches

Is there any way to see the result in the output like a normal grep search?

1
  • 3
    What output do you expect from grep?
    – anubhava
    Commented May 7, 2014 at 8:50

1 Answer 1

412

Try:

grep --text

or

grep -a 

for short. This is equivalent to --binary-files=text and it should show the matches in binary files.

3
  • 12
    Note that you may need this flag in case your input file is indeed text file but it contains e.g. some random binary bytes in the middle because the data is corrupted or the "binary content" heuristics fails otherwise. The intent to need to specify this flag is to avoid outputting raw binary content to output accidentally if you grep a binary file by mistake. Traditionally outputting binary content to terminal could mess the whole terminal and require resetting the terminal to continue but that shouldn't happen with modern UTF-8 based systems. Commented Sep 21, 2022 at 10:44
  • 6
    -a, --text Process a binary file as if it were text; this is equivalent to the --binary-files=text option.
    – Danijel
    Commented Jan 31, 2023 at 7:51
  • Strangely, I ran into this when trying to grep .bash_history. --text worked, but I wanted to know why an obvious text file was detected as binary. I opened it in xed which complained about invalid characters and found a line highlighted red with \00 repeated 140 times before a tmux command. No idea how that got there, but deleting the \00s resolved it.
    – Pilot_51
    Commented Nov 30, 2024 at 17:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.