cmp: Byte-for-Byte File Comparison
cmp compares two files byte by byte and reports the byte and line number of the first difference, working equally well on binary and text files.
When you only need to know whether two files are identical (especially binaries), cmp is faster and simpler than diff. Its exit code makes it a clean CI gate.
What it does
cmp reads two files and stops at the first differing byte, printing its byte offset and line number. With -s it prints nothing and just sets the exit code: 0 if identical, 1 if they differ, 2 on error. It works on binary files where diff would be useless.
Common usage
cmp file1.bin file2.bin
# silent: use only the exit code as a gate
cmp -s built.bin expected.bin && echo "identical"
# list every differing byte
cmp -l a.bin b.binOptions
| Flag | What it does |
|---|---|
| -s | Silent: no output, set exit code only |
| -l | List the byte offset and octal values of every difference |
| -n <n> | Compare at most n bytes |
| -i <n> | Skip the first n bytes of both files |
| exit 0/1/2 | Identical / differ / error |
In CI
For checksummed or binary artifacts, cmp -s built expected is a precise gate: exit 1 means the bytes differ, failing the step. Use cmp instead of diff when the files are not text, because diff will only say "Binary files differ".
Common errors in CI
"cmp: EOF on file1" (or file2) means one file is a proper prefix of the other: identical up to that point but shorter; that still counts as differing (exit 1). "cmp: file1: No such file or directory" is exit 2, an error, not a content difference; distinguish it from exit 1 in scripts. The reported line number is 1-based and counts newlines, which can surprise you on files without trailing newlines.