TIL: byte-by-byte file comparison in shell scripts with cmp
Original title: TIL: cmp: Fast Byte-by-Byte File Comparison for Shell Scripts
To check whether two files are identical in a shell script, use cmp instead of diff: cmp compares byte by byte and stops at the first mismatch, finishing in milliseconds even for GB-sized files, while diff has to parse the whole file line by line. cmp's -s silent mode reports the result through its exit code: 0 means the files are identical, 1 means they differ, and 2 means a file is missing or unreadable. cmp suits binary files and automation scripts, and ships by default in POSIX environments.
October 4, 2026 2 min read
Today I stopped using diff to check whether two files match in shell scripts.
My old default check was diff -q file1 file2 > /dev/null. That command solves a different problem. diff parses text line by line to calculate edit hunks. On large disk images, database dumps, or compiled binaries, diff reads entire files and consumes unnecessary CPU cycles.
For a boolean equality check, cmp reads byte by byte and halts at the first mismatch.
The Basic Command
Running cmp without flags prints the first point of divergence:
cmp config-old.json config-new.jsonIf the files match, cmp produces no output. If they differ, it pinpoints the exact byte offset and line:
config-old.json config-new.json differ: byte 25, line 3Because cmp halts at the first mismatch, it finishes in milliseconds even on gigabyte-sized files.
The Shell Script Pattern
The -s (silent) flag suppresses standard output and error streams. The command exits with three status codes for shell conditions:
0: Files are identical1: Files differ2: Trouble encountered (file missing or unreadable)
if cmp -s config.new /etc/app/config.json; then
echo "Configuration unchanged. Skipping reload."
else
echo "Configuration changed. Applying update..."
cp config.new /etc/app/config.json
systemctl restart app
fiThe if block checks the exit code. You drop boilerplate redirects like > /dev/null 2>&1.
cmp vs diff
| Command | Comparison Method | Halts At | Best For |
|---|---|---|---|
cmp | Byte by byte | First mismatch | Equality checks, binary files, shell automation |
diff | Line by line | End of file | Reviewing human-readable text edits |
Quick Reference
| Flag | Purpose | Example |
|---|---|---|
-s | Run silent and return an exit code without output | cmp -s file1 file2 |
-l | Print byte offset and differing octal values | cmp -l file1 file2 |
-b | Print differing bytes as readable characters | cmp -b file1 file2 |
-n N | Compare at most N bytes | cmp -n 1024 disk1.img disk2.img |
Common Workflows
- Backup validation: Verify that a restored database dump matches the original byte for byte:
cmp -s db-backup.sql /mnt/storage/db-backup.sql - Binary artifact checks: Verify that firmware images or compiled binaries match upstream builds:
cmp app-linux-x64 dist/app-linux-x64 - Build pipeline triggers: Prevent downstream rebuild steps by checking if generated asset files changed before writing them.
What I Learned
cmphalts at the first mismatch, leaving the rest of the file unread.- The
-sflag turnscmpinto a boolean test for shell scripts. - The command compares binary assets where
diffissues warnings or chokes on null bytes. - Exit code
2flags missing or inaccessible files, separating system errors from content diffs. - POSIX environments include
cmpby default, requiring no third-party package.
Source: Kondasamy Jayaraman · Engineering Blog · kondasamy.com