Interview Prep/Shell Scripting/Troubleshooting: tools, portability, permissions and environment

`grep ERROR /var/log/app/app.log` prints only `grep: /var/log/app/app.log: binary file matches`.

BeginnerTroubleshootingTroubleshooting: tools, portability, permissions and environment
Answer

Likely causes → The log contains NUL bytes or invalid UTF-8 (a crash mid-write, a binary payload logged by mistake, or a file preallocated with zeros after a copytruncate), so GNU grep treats it as binary and suppresses matching lines.

Diagnose →

grep -c $'\x00' /var/log/app/app.log 2>/dev/null; tr -d '\000' < /var/log/app/app.log | wc -l
grep -naP '\x00' /var/log/app/app.log | head -3       # where the NULs are (GNU grep)

Fix → Force text mode, optionally stripping NULs:

grep -a ERROR /var/log/app/app.log
tr -d '\000' < /var/log/app/app.log | grep ERROR

Prevent → Use -a in scripts that grep logs, and fix the root cause: copytruncate with an app that does not open the file in append mode leaves a sparse, NUL-filled prefix; switch to postrotate with a reopen signal.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: tools, portability, permissions and environment