cmp Command Guide for Byte-Level File Comparison
cmp compares two files byte by byte and reports the first difference it finds — the tool to reach for when diff (a line-based tool) isn’t enough, like with binary files.
What is cmp, and Why Use It?
cmp (“compare”) reads two files and compares them byte by byte, not line by line. It works on any file — text or binary — and by default prints the byte position and the differing values of the first mismatch only.
Where diff answers “which lines changed?”, cmp answers “are these files exactly identical, and if not, where does the difference start?”
Why Learn cmp?
- You need a guaranteed-identical check.
cmpis exact; two files that passcmpare byte-for-byte the same. - You’re working with binaries — compiled files, images, archives — where “lines” are meaningless.
- You want the first point of divergence, which can reveal corruption or a truncated copy.
- Scripts use it as the strictest test:
cmp -s a b && echo identical.
diffandcmpoverlap for text, but they answer different questions.diffis for reviewing changes;cmpis for proving equality and finding the exact mismatch point in anything.
1. Compare Two Files
cmp file1.bin file2.bin
- If the files differ, prints the byte offset (and line/column) of the first difference plus the differing bytes:
file1.bin file2.bin differ: char 1048, line 66
- If they are identical, prints nothing and exits
0.
2. Compare Silently (Script Mode)
cmp -s file1.bin file2.bin && echo "identical" || echo "different"
-s/--silentsuppresses all output; only the exit code matters.- Exit codes:
0identical,1different,2trouble. - This is the canonical one-liner for “are these two downloads the same?” in scripts.
3. Compare Only the First N Bytes
cmp -n 4096 file1.bin file2.bin
- Stops after comparing 4096 bytes — useful for comparing huge files where you only care about the header (e.g. two ISO images that should share a boot block).
4. Skip a Known Prefix
cmp -i 512 file1.bin file2.bin
- Skips the first 512 bytes of both files before comparing — handy when files share a known prefix (headers, timestamps) but should match after it.
5. Compare All Differences, Not Just the First
cmp -l file1.bin file2.bin
-l(verbose) lists every differing byte: offset, and the octal value of each file’s byte.- Output like
1048 61 62means: at byte 1048, file1 has octal61and file2 has octal62. - For long files this can be enormous — pipe through
headfirst.
6. Compare Two Directories’ Files
cmp -r dir1 dir2
- Recursively compares files with the same names in both trees, like a byte-strict diff -r.
7. Quick Identical-Check for Big Files
cmp file1.iso file2.iso
- For large files, a fast “is this copy complete?” check — far quicker than hashing whole files when you expect an answer at the first divergence.
- If you need a portable fingerprint instead of a comparison, use sha256sum on each file.
Common Pitfalls
- No output on identical files is success, not an error.
cmpprints nothing when files match; checkecho $?if unsure. cmpexits 1 for “differ” — scripts withset -emust handle it:cmp -s a b || echo differs.- Binary files make
diffnearly useless (“Binary files differ” with no detail). That’s exactly whatcmp -lis for. - Byte offsets count from 1. When piping output into other tools, remember the reported position includes the skipped prefix if you used
-i.
Related Commands
| Command | Description |
|---|---|
| diff | Line-based comparison with hunks and patches |
| comm | Compare two sorted files line by line |
| sha256sum | Fingerprint a file for later comparison |
| cksum | Quick CRC checksum of a file |
cmp Cheatsheet
| Command | What It Does |
|---|---|
cmp a.bin b.bin | Compare two files byte by byte |
cmp -s a b && echo same || echo diff | Silent identical-check (exit code only) |
cmp -l a b | List every differing byte (offset + octal values) |
cmp -n 1000 a b | Compare only the first 1000 bytes |
cmp -i 512 a b | Skip the first 512 bytes, then compare |
cmp -r dir1 dir2 | Recursively compare same-named files |
echo $? after cmp | 0 identical, 1 differ, 2 error |