Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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. cmp is exact; two files that pass cmp are 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.

diff and cmp overlap for text, but they answer different questions. diff is for reviewing changes; cmp is 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 / --silent suppresses all output; only the exit code matters.
  • Exit codes: 0 identical, 1 different, 2 trouble.
  • 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 62 means: at byte 1048, file1 has octal 61 and file2 has octal 62.
  • For long files this can be enormous — pipe through head first.

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. cmp prints nothing when files match; check echo $? if unsure.
  • cmp exits 1 for “differ” — scripts with set -e must handle it: cmp -s a b || echo differs.
  • Binary files make diff nearly useless (“Binary files differ” with no detail). That’s exactly what cmp -l is for.
  • Byte offsets count from 1. When piping output into other tools, remember the reported position includes the skipped prefix if you used -i.

CommandDescription
diffLine-based comparison with hunks and patches
commCompare two sorted files line by line
sha256sumFingerprint a file for later comparison
cksumQuick CRC checksum of a file

cmp Cheatsheet

CommandWhat It Does
cmp a.bin b.binCompare two files byte by byte
cmp -s a b && echo same || echo diffSilent identical-check (exit code only)
cmp -l a bList every differing byte (offset + octal values)
cmp -n 1000 a bCompare only the first 1000 bytes
cmp -i 512 a bSkip the first 512 bytes, then compare
cmp -r dir1 dir2Recursively compare same-named files
echo $? after cmp0 identical, 1 differ, 2 error