A log export contains text files, a dump, and sometimes NULL bytes. When GNU grep displays “binary file matches” instead of the lines, it hasn’t necessarily frozen: it indicates that a match exists but hides the output details. To search only in text logs, choose -I. To deliberately examine a file containing non-text bytes, choose -a and inspect its output without sending it raw to the terminal. The two options serve opposite needs.
Understanding the “binary file matches” message before changing the command
GNU grep may treat input as binary when it finds a NULL byte or invalid output data for the locale. In default binary mode, it may then replace match details with the message “binary file matches.” This message neither proves a freeze, nor corruption, nor the absence of a match.
Start by verifying that the target is indeed the expected file, that it is readable, and that it is a regular file. Replace application.log with a path you have confirmed:
grep --version
locale
test -f application.log && test -r application.log
stat -c '%n : %s bytes, rights %a' -- application.log
file -- application.log
od -An -tx1 -N 64 -- application.log
grep --version confirms that you are using GNU grep and indicates its version. locale allows you to see the active character environment. file gives an indication of format, not a certificate of content or the same heuristic decision as grep. od displays the first bytes in hexadecimal: 00 corresponds to a NULL. The absence of 00 in these first 64 bytes does not exclude a NULL further on. stat is here the GNU variant; file may not be installed.
If you need to identify the file type before choosing the mode, identify its format with file. For a FIFO or an open stream provided on standard input, the diagnostic is different: -D skip does not end the wait for a stream already provided on stdin.
Ignore binary data with -I
To search for a pattern in only the contents that GNU grep does not treat as binary, -I is the bounded choice. This option is equivalent to --binary-files=without-match: an input recognized as binary is considered to contain no matches.
rc=0
if grep -InF -e 'ERROR' -- application.log; then
rc=0
else
rc=$?
fi
printf 'Grep code: %sn' "$rc"
-I is the capital I, not -i, which relates to case. Here, -n displays line numbers and -F treats ERROR as literal text. The equivalent long form is:
grep --binary-files=without-match -nF -e 'ERROR' -- application.log
The usual codes for GNU grep are 0 if a line was selected, 1 if no line was selected, and 2 in case of error. A code 1 with -I does not therefore prove that the pattern is absent from the bytes of the file: grep may have found a NULL and ignored the rest of the input. Depending on the buffer, previous lines may already have been displayed. Do not hide error diagnostics to make the result cleaner.
I would keep -I to search in text logs within a mixed folder, knowing that this choice may exclude a useful log. Detection is a heuristic of grep, influenced particularly by options and locale, not an infallible classification of formats.
Force the search with -a without displaying raw bytes
-a is equivalent to --binary-files=text: grep forces the processing of the input as text. Use it only to deliberately examine a specific file. It does not eliminate NULL, does not repair content, and does not decode a UTF-16 file. NULL bytes or control sequences may remain in the selected lines.
Do not therefore launch grep -a alone on unknown input while letting its raw result display in a terminal. Pipe the output through od:
LC_ALL=C grep -anF -e 'ERROR' -- application.log | od -An -tx1 -v
codes=("${PIPESTATUS[@]}")
printf 'grep: %s; od: %sn' "${codes[0]}" "${codes[1]}"
In an interactive Bash, retrieve PIPESTATUS immediately after the pipeline, without interposing another command. The array separately gives the status of grep and that of od; the single global status of the pipeline is not enough to interpret the two steps. Here, LC_ALL=C applies to grep and the pattern is ASCII. The hexadecimal display avoids passing raw control bytes to the terminal but remains less readable for large content.
-I and -a are incompatible as processing choices. Do not combine them to pretend to ignore and include binary data at the same time. Also choose --binary-files=without-match or --binary-files=text if you prefer to specify the mode, without adding both types together.
Limit recursive search and excluded files
When the target is a directory, use -r with a specific path. Avoid -R by default: -r does not follow symbolic links encountered in the tree, while -R follows all of them and can thus read outside the visible target. -D skip asks grep to ignore devices, FIFO, and sockets encountered as files to read.
grep -rInHF -D skip --exclude='*.dump' --exclude-dir='archive' -e 'ERROR' -- ./logs
-H forces the display of the path, -n gives the line number, and -F keeps the literal pattern. Adapt the path and exclusions to the tree you have verified. The quotes protect the globs from being expanded by Bash. --exclude targets file names and --exclude-dir targets directory names; these are not regular expressions applied to the content. They do not constitute a security boundary, especially if you choose -R. If .gz archives are present, exclude them explicitly: this command does not decompress them.
To cap the number of lines selected per file, you can add -m 20. This is neither a maximum time limit nor a global data read limit; a search without a match can still traverse a lot of content. Do not present this variant as exhaustive.
Check the locale when a text log is treated as binary
LC_CTYPE defines the encoding used to interpret characters; LC_ALL, when set, overrides locale categories. An invalid sequence for the locale may trigger binary processing even without NULL. In locale C or POSIX, each byte is a valid character: the interpretation may therefore change.
For a precise ASCII pattern, a limited local test may help compare behavior:
LC_ALL=C grep -nF -e 'ERROR' -- application.log
LC_ALL=C does not convert the file and does not correct its encoding. Case, character classes, and Unicode semantics may change. NULLs remain relevant for binary processing unless you choose -a or another suitable mode. Do not export this locale globally as a universal fix.
Control the output before concluding the absence of error
In a new workshop, you can reproduce the case with synthetic files. The creation commands below only serve for this isolated workshop, never for an existing log directory. First, make sure TMPDIR points to an existing and trusted location; otherwise, the home directory will be used:
workshop=$(mktemp -d "${TMPDIR:-$HOME}/grep-binary.XXXXXX") || exit 1
cd -- "$workshop" || exit 1
mkdir -- archive
printf 'INFO startnERROR readablen' > text.log
printf 'INFO startnERROR before 00afternERROR aftern' > nul.log
printf 'ERROR byte 377n' > invalid.log
printf 'ERROR oldn' > archive/old.log
printf 'ERROR dumpn' > secret.dump
ln -s -- archive/old.log link.log
These redirections create the fixtures for the workshop; they should not be copied with a path to real logs. printf and the escapes are intended for Bash. On this fixture, the expected comparison is:
LC_ALL=C.UTF-8 grep -nF -e 'ERROR' -- nul.log
LC_ALL=C.UTF-8 grep -InF -e 'ERROR' -- nul.log
LC_ALL=C grep -anF -e 'ERROR' -- nul.log | od -An -tx1 -v
LC_ALL=C.UTF-8 grep -rInHF -D skip --exclude='*.dump' --exclude-dir='archive' -e 'ERROR' -- .
The first call reports a binary match on stderr, with code 0. The second selects no line and returns 1. In the hexadecimal output of the third, 00 remains visible: -a has not cleaned anything. The recursive traversal displays ./text.log:2:ERROR readable, not the contents of archive/ or secret.dump, and returns 0. C.UTF-8 must exist on the concerned system; do not assume its availability everywhere.
For invalid.log, the comparison between C.UTF-8 and C shows that interpretation depends on the locale: in C.UTF-8, byte ff is invalid and may cause the input to be treated as binary; in C, the line is selected with this byte preserved. Any output likely to contain control bytes should remain inspected via od, never displayed raw.
Once a text log is identified, you can re-read an error with its context; to browse this text file, re-read the log with less. These steps do not change the choice made for the binary input.
On your own files, remember the codes 0, 1, or 2, the standard output, and stderr diagnostics together. Do not conclude the absence of error based solely on the empty result of -I. If a search starts on a target too broad, interrupt it with Ctrl+C and then narrow the path; do not delete anything and do not kill any service. This control remains limited to the files and traversal effectively read: it does not certify either the data ignored as binary, or the excluded archives, or the absence of match in an untraversed stream.
Technical sources: – GNU Grep manual 3.12, file and directory selection – GNU Grep manual 3.12, character encoding – GNU Grep manual 3.12, exit status – GNU grep Manpage