Skip to content

Avoid "binary file matches" on Log Files

Updated: at 05:32 AM

When you’re looking through log files to fix bugs or figure out what’s happening in your software, you might use a tool called grep. It’s great for finding specific pieces of text in files. But sometimes, grep tells you “binary file matches” instead of showing you the text you’re looking for. This happens because the file has stuff in it that grep thinks isn’t regular text, like emojis or certain symbols.

The Problem

Imagine you’re using grep to search for the word “reset” in log files that start with access, but all you get is a message saying “Binary file access.log matches”. This means grep thinks access.log is a binary file, not a text file, so it doesn’t show you the matching lines of text. This can be annoying because you know there’s useful information in there, but grep won’t show it to you.

A Solution

You can tell grep to treat these so-called binary files as text files. This way, it will search through them and show you the lines of text that match your search. You do this by adding --binary-files=text to your grep command, like this:

grep --binary-files=text wp-admin access*

This command searches for “wp-admin” in files that start with access, and tells grep to treat them as text files. Now, instead of just telling you there’s a match, it will actually show you the matching text.

This trick is really useful when you’re dealing with log files that for some reason grep thinks are binary. It could be because there are emojis in the file, or because of other special characters. This way, you don’t miss out on important information that could help you fix a problem or understand what’s happening in your system.