Skip to content

Fix grep "binary file matches": Why grep Prints Nothing and Exits 0

greptroubleshootinglogsexit-codesencoding
7 min read
Matching toolgrep Pattern Builder

Quick Answer

grep: app.log: binary file matches means grep found your pattern but decided the file is binary, so it printed that one line instead of the matching lines. GNU grep 3.12 calls a file binary when it contains a NUL byte, or when a matching line has bytes that are invalid in the current locale, such as a Latin-1 é under en_US.UTF-8. The message goes to stderr and grep exits 0, so line=$(grep ok app.log) succeeds with an empty $line and set -euo pipefail does not stop it. grep 3.4 and older printed Binary file app.log matches on stdout instead, so the same line captured the message as data. To print the lines, add -a or --binary-files=text. -I skips binary files: silent, exit 1. To find the bad byte, grep -naP '\x00' app.log; without -a, grep -cP '\x00' reports 0 even when the NUL is there.

Build the grep line first

The Grep Pattern Builder builds the command and its flags. This page is about the one case where a correct grep prints nothing.

The health check greps the log for job ok and stores the last match. One night the variable came back empty, the exit status was 0, and the check reported nothing useful. The log was fine except for one line where a worker wrote a NUL byte. One byte in one line made grep call the whole file binary, and grep's response to a binary match is to say so on stderr and print none of the lines.

What Does "binary file matches" Mean?

Reproduced here on 2026-11-03 (Kali 2026.3, GNU bash 5.3.15, GNU grep 3.12, LANG=en_US.UTF-8). app.log has three lines; the middle one holds a NUL:

text
$ printf '2026-11-03 10:00:01 job ok\n2026-11-03 10:00:02 worker\000restart\n2026-11-03 10:00:03 job ok\n' > app.log $ grep ok app.log grep: app.log: binary file matches exit=0 $ grep ok app.log 2>/dev/null | wc -c 0

Two lines match. grep printed neither, wrote one line to stderr, and exited 0. With stderr hidden there are zero bytes of output. Note that the matching lines are clean: the NUL is in the line that does not match.

Why Does line=$(grep ...) Succeed with an Empty Value?

text
$ line=$(grep ok app.log); echo "exit=$? line=[$line]" grep: app.log: binary file matches exit=0 line=[]

That is the surprise: it is not an error. Exit 0 means "found a match", which is true. $(...) captures stdout only, and stdout is empty. set -euo pipefail has nothing to catch, because no command failed, as this script shows:

bash
#!/bin/bash # Script: before.sh # Purpose: Show grep returning success with nothing captured on a log with a NUL # Usage: ./before.sh [LOG_FILE] (default: app.log) set -euo pipefail CHECK="✓" CROSS="✗" LOG_FILE="${1:-app.log}" line=$(grep "job ok" "$LOG_FILE" | tail -n 1) if [[ -z $line ]]; then echo "$CROSS line is empty"; else echo "$CHECK last: $line"; fi
text
$ bash before.sh grep: app.log: binary file matches ✗ line is empty exit=0

Under cron, stderr usually goes to a mail spool or nowhere, so the only trace is the empty value. grep -c ok app.log (prints 2), grep -q and grep -l all work normally on the same file; only the commands that print lines are affected.

Did Older grep Behave Differently?

Yes, and the change breaks code in the other direction. The same file in an ubuntu:20.04 container (GNU grep 3.4, bash 5.0.17):

text
$ grep ok app.log Binary file app.log matches exit=0 $ line=$(grep ok app.log); echo "exit=$? line=[$line]" exit=0 line=[Binary file app.log matches] $ grep ok app.log 2>/dev/null Binary file app.log matches

grep 3.4 wrote Binary file app.log matches to stdout, so the message itself was captured as the matching line. GNU grep 3.5 moved it to stderr and changed the wording. ubuntu:24.04 (grep 3.11) and ubuntu:26.04 (grep 3.12) both behave like Kali: stderr, empty $line, exit 0. A script that parsed the old output on a 20.04 host gets an empty string after the upgrade.

How Do I Make grep Print the Lines?

-a or its long form --binary-files=text:

text
$ grep -a ok app.log 2026-11-03 10:00:01 job ok 2026-11-03 10:00:03 job ok exit=0 $ grep --binary-files=text ok app.log 2026-11-03 10:00:01 job ok 2026-11-03 10:00:03 job ok exit=0

For a script, put -a in the capture and check for an empty result anyway:

bash
#!/bin/bash # Script: last-ok.sh # Purpose: Get the last "job ok" line even when the log has a stray NUL byte # Usage: ./last-ok.sh [LOG_FILE] (default: app.log) set -euo pipefail CHECK="✓" CROSS="✗" LOG_FILE="${1:-app.log}" PATTERN="job ok" # -a: a NUL in one line must not hide every other line (grep calls the file binary) line=$(grep -a -- "$PATTERN" "$LOG_FILE" | tail -n 1) if [[ -z $line ]]; then echo "$CROSS no '$PATTERN' line in $LOG_FILE" >&2 exit 1 fi # tr strips NULs so a binary byte never reaches the terminal or a downstream parser echo "$CHECK last: $(printf '%s' "$line" | tr -d '\000')"
text
$ bash last-ok.sh ✓ last: 2026-11-03 10:00:03 job ok exit=0

-a prints a matching line with its bad bytes intact, which is why the script strips NULs before printing. Another route is to clean the stream first: tr -d '\000' < app.log | grep ok printed both lines and exited 0.

When Should I Use grep -I Instead?

When the binary files are real binaries you want skipped, not logs you want read. -I (--binary-files=without-match) treats a binary file as having no match:

text
$ grep -I ok app.log exit=1 $ grep -r ok . ./latin1.log:2026-11-03 10:00:01 job ok ./latin1.log:2026-11-03 10:00:03 job ok grep: ./app.log: binary file matches exit=0 $ grep -rI ok . ./latin1.log:2026-11-03 10:00:01 job ok ./latin1.log:2026-11-03 10:00:03 job ok exit=0

On one file, -I is silent with exit 1: the log's two job ok lines are now reported as "no match". Across a tree with -r, it drops the noise from images and compiled files. Do not use it on logs.

How Do I Find the Byte That Caused It?

The obvious check gives the wrong answer:

text
$ grep -c -P '\x00' app.log 0 exit=1 $ grep -c -a -P '\x00' app.log 1 exit=0 $ grep -naP '\x00' app.log | cat -v 2:2026-11-03 10:00:02 worker^@restart $ od -c app.log | grep -F '\0' 0000060 o r k e r \0 r e s t a r t \n 2 0

Without -a, grep handles the file as binary and treats NUL as a line terminator, so no line ever contains a NUL and the count is 0, exit 1: "no NUL here". Add -a. -n gives the line number and cat -v shows the NUL as ^@.

Can a Non-NUL Byte Trigger It?

Yes, and only in a UTF-8 locale. latin1.log is the same log with café written as a single Latin-1 byte (0xE9) and no NUL:

text
$ grep ok latin1.log 2026-11-03 10:00:01 job ok 2026-11-03 10:00:03 job ok exit=0 $ grep restart latin1.log grep: latin1.log: binary file matches exit=0 $ LC_ALL=C grep restart latin1.log | cat -v 2026-11-03 10:00:02 cafM-i restart exit=0 $ LC_ALL=C grep ok app.log grep: app.log: binary file matches exit=0

Unlike the NUL case, the invalid byte only matters on a line grep would print: ok lines come through, the restart line does not. LC_ALL=C makes every byte valid, so the line prints. It does nothing for a NUL. In the Ubuntu containers, which have no LANG set and run in the C locale, grep restart latin1.log printed the line with no message, while LC_ALL=C.UTF-8 brought the message back. That is how a grep can work in a cron job or container and fail at an interactive shell, or the reverse. To list the lines with invalid bytes, stay in the UTF-8 locale and ask for lines that are not entirely valid characters:

text
$ grep -naxv '.*' latin1.log | cat -v 2:2026-11-03 10:00:02 cafM-i restart exit=0 $ LC_ALL=C grep -naxv '.*' latin1.log exit=1

In the C locale every byte matches ., so the same command finds nothing. It also finds nothing in app.log: a NUL is a valid character, which is why the NUL check above needs its own pattern.

Tested On

Kali 2026.3 (bash 5.3.15, GNU grep 3.12, en_US.UTF-8); ubuntu:24.04 (bash 5.2.21, GNU grep 3.11) and ubuntu:26.04 (bash 5.3.9, GNU grep 3.12) containers, C locale: same stderr message, exit 0 and empty $line, -I exit 1, grep -c -a -P '\x00' prints 1. ubuntu:20.04 (bash 5.0.17, GNU grep 3.4): old stdout message, captured into $line.

Frequently Asked Questions

Why does grep say binary file matches on a text log?

Because one byte in it is not text. In the runs here, a NUL byte anywhere in the file, or a byte that is invalid in your locale's encoding on a line it would print, marks the file binary. Logs pick these up from a crashed writer, a preallocated or truncated file, a program that logs raw binary fields, or a Latin-1 character in a UTF-8 locale. The other lines are still plain text: grep -a prints them.

Is binary file matches an error?

No. grep exits 0, the same as a normal match, because it did find the pattern. The message is written to stderr, not stdout, since GNU grep 3.5. That is the trap: a script that captures stdout gets an empty string, sees success, and has nothing to tell it the data was withheld. Check for an empty result, or use -a so the lines come through.

What is the difference between grep -a and grep -I?

-a (--binary-files=text) treats every file as text and prints the matching lines, NUL bytes included. -I (--binary-files=without-match) treats binary files as having no match at all: nothing printed, no message, and exit 1 for a single file. Use -a when the file is a log with a stray bad byte and you want its lines. Use -I with grep -r over a tree that holds real binaries such as images or compiled files you want to skip.

Why does grep -c -P '\x00' say 0 when the file has a NUL?

Without -a, grep handles a binary file differently: GNU grep may treat NUL bytes as line terminators, so no line ever contains one and the pattern cannot match. Tested here: grep -c -P '\x00' app.log printed 0 and exited 1 on a file with one NUL, while grep -c -a -P '\x00' app.log printed 1. Add -a, and -n to get the line number, when searching for the byte itself.

Does LC_ALL=C fix binary file matches?

Only when the cause is an encoding error. With LANG=en_US.UTF-8, a line containing a lone 0xE9 byte made grep report binary file matches; LC_ALL=C grep printed the line, because in the C locale every byte is valid. A NUL byte is binary in every locale: LC_ALL=C grep ok app.log still printed the message. Cron and containers often run in the C locale, which is why the same grep can behave differently there.

The safe bash script template checks captured values before using them, which is the check that catches this one, and The Production Bash Toolkit ships that template ShellCheck-clean.


Part of the bash snippets collection

PAID RESOURCE — $9

The Production Bash Toolkit

An operational script system + a 30-function shared library + a 52-page field guide. The production layer the free snippets don't cover.

Get the Toolkit →
curl -O bashlib-starter.sh

Get the bashlib starter

Ten functions I source into every script on my own boxes — strict-mode setup, an ERR trap that names the failing line, lock and timeout wrappers, and cleanup that runs on every exit path. After that, a short email only when something breaks on a real box and I've worked out why, about twice a month. One-click unsubscribe.

BashSnippets logo

Written by Travis

Creator of BashSnippets.xyz

bashsnippets.xyz/about

Related Snippets

Frequently Asked Questions

faq — snippet

Why does grep say binary file matches on a text log?

Because one byte in it is not text. In the runs here, a NUL byte anywhere in the file, or a byte that is invalid in your locale's encoding on a line it would print, marks the file binary. Logs pick these up from a crashed writer, a preallocated or truncated file, a program that logs raw binary fields, or a Latin-1 character in a UTF-8 locale. The other lines are still plain text: grep -a prints them.

faq — snippet

Is binary file matches an error?

No. grep exits 0, the same as a normal match, because it did find the pattern. The message is written to stderr, not stdout, since GNU grep 3.5. That is the trap: a script that captures stdout gets an empty string, sees success, and has nothing to tell it the data was withheld. Check for an empty result, or use -a so the lines come through.

faq — snippet

What is the difference between grep -a and grep -I?

-a (--binary-files=text) treats every file as text and prints the matching lines, NUL bytes included. -I (--binary-files=without-match) treats binary files as having no match at all: nothing printed, no message, and exit 1 for a single file. Use -a when the file is a log with a stray bad byte and you want its lines. Use -I with grep -r over a tree that holds real binaries such as images or compiled files you want to skip.

faq — snippet

Why does grep -c -P '\x00' say 0 when the file has a NUL?

Without -a, grep handles a binary file differently: GNU grep may treat NUL bytes as line terminators, so no line ever contains one and the pattern cannot match. Tested here: grep -c -P '\x00' app.log printed 0 and exited 1 on a file with one NUL, while grep -c -a -P '\x00' app.log printed 1. Add -a, and -n to get the line number, when searching for the byte itself.

faq — snippet

Does LC_ALL=C fix binary file matches?

Only when the cause is an encoding error. With LANG=en_US.UTF-8, a line containing a lone 0xE9 byte made grep report binary file matches; LC_ALL=C grep printed the line, because in the C locale every byte is valid. A NUL byte is binary in every locale: LC_ALL=C grep ok app.log still printed the message. Cron and containers often run in the C locale, which is why the same grep can behave differently there.