Grep Reading Apache Error Log as Binary

An issue was recently noted where ‘grep‘ was throwing the error:

root@host [~]# zgrep "ajax" /usr/local/apache/logs/error_log
Binary file (standard input) matches

The permissions were checked, and several other ‘grep‘ type commands were attempted and all returned the same error. In addition, the ‘head‘, ‘tail‘ and ‘less‘ commands all returned the normal log information to standard output.

In reviewing the issue, if there is a NUL character anywhere in the file, ‘grep‘ (or zgrep, egrep…etc.) will consider it as a binary file.

To solve this, use the ‘-r‘ or – – text flags to process the binary file as if it were standard text; these flags are equivalent to the –binary-files=text option.

egrep -r -a mystring


grep -a/--text

The downside to this is that the data returned may well result in binary information being sent to your terminal. This is not a good idea if you’re running a terminal such as VT/DEC or many others that interprets the output stream. Alternatively, you can pipe/output the file through ‘tr’ with the following command:

tr '[\000-\011\013-\037\177-\377]' '.'
which will change anything less than a space character (except newline) and anything greater than 126, into a . character, leaving only the printable characters.



I am a g33k, Linux blogger, developer, student and Tech Writer for My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.