>_cmd.script

cmp

Compare two files byte by byte

Text Processing

By CMD Script Team · 4 min read · Last updated

SYNTAX
cmp [OPTIONS] FILE1 FILE2

Options

Command options and flags
FlagDescription
-sSilent mode: print nothing, only set the exit status (0 = identical, 1 = differ)
-lList every byte position and differing byte values, instead of stopping at the first difference
-bPrint differing bytes in a human-readable form alongside their offsets
-i NUMSkip the first NUM bytes of both files before starting the comparison
-n NUMCompare at most NUM bytes

Distribution compatibility

  • Ubuntu
  • Debian
  • Fedora
  • Arch
  • macOS

What it does

cmp compares two files byte by byte and, by default, reports the position (byte and line number) of the first place they differ. Unlike diff, which works at the level of lines and is built for readable text comparisons, cmp works at the level of raw bytes, making it the right tool for verifying whether two binary files — images, archives, executables — are exactly identical.

Beginner examples

  • cmp file1.txt file2.txt — report the first byte where the two files differ
  • cmp file1.bin file2.bin — compare two binary files
  • cmp -s file1 file2 — silently compare, useful for checking the exit status in scripts
  • If the files are identical, cmp prints nothing and exits with status 0
cmp original.iso downloaded.iso

Advanced examples

  • Use in a script to branch on whether files match: if cmp -s a.txt b.txt; then echo "same"; else echo "different"; fi
  • List every byte offset and value that differs between two files: cmp -l file1.bin file2.bin
  • Skip a fixed-size header before comparing raw data: cmp -i 512 disk1.img disk2.img
  • Limit comparison to only the first N bytes of each file: cmp -n 1024 file1 file2
  • Verify a downloaded file exactly matches a known-good copy before deploying it: cmp -s release.tar.gz release.tar.gz.verified && echo "verified"
cmp -l firmware_v1.bin firmware_v2.bin | wc -l

Common mistakes

  • Using cmp on text files when you actually want a readable, line-based diff — diff is far more useful there since cmp just reports byte offsets, not meaningful line changes.
  • Forgetting -s in scripts and having to parse cmp's human-readable output instead of just checking its exit status.
  • Expecting cmp (without -l) to show every difference — by default it stops at the very first one.
  • Comparing files of different lengths and being confused by the "EOF on file1" message — cmp reports when one file ends before the other, which counts as a difference too.

Tips

  • cmp's exit status (0 = identical, 1 = different, 2 = error) makes it ideal for conditionals in shell scripts: cmp -s a b && echo same.
  • For a byte-for-byte integrity check (e.g. verifying a copy or transfer), cmp is faster and simpler than computing and comparing checksums for small-to-medium files.
  • Combine cmp -l with wc -l to get a quick count of how many bytes differ between two files.

Best practices

  • Use cmp -s inside scripts for identical/different checks; reserve the default verbose mode for interactive debugging where you want to see exactly where files diverge.
  • Prefer diff over cmp for source code and configuration files, since line-based output is far more actionable for humans reviewing changes.
  • For verifying large file transfers or backups, consider whether a checksum tool (sha256sum) might be more appropriate than a full byte-by-byte cmp, especially over slow storage, since checksums can be computed once and stored for repeated comparison.

Try it yourself

A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.

Real-world use cases

  • Verifying that a downloaded binary or ISO image is byte-for-byte identical to a known good reference copy.
  • Confirming two builds of a compiled binary are identical (or not) after a supposedly no-op refactor.
  • Checking a backup restoration produced an exact copy of the original file before discarding the backup.

Common interview questions

  • What's the difference between cmp and diff? cmp compares files byte by byte and reports the first differing byte/line; diff compares files line by line and produces a readable changeset of insertions, deletions, and modifications, making it better suited to text while cmp suits binary data.
  • How would you check if two files are identical inside a shell script? cmp -s file1 file2 and check the exit status — 0 means identical, non-zero means they differ or an error occurred.
  • Why would cmp be preferred over diff for binary files? cmp works at the byte level and doesn't try to interpret content as lines of text, so it correctly and efficiently handles binary data that diff would otherwise report as one giant "differs" blob without useful detail.

Frequently Asked Questions

What's the difference between cmp and diff?

cmp compares files byte by byte and reports the position of the first difference (or, with -l, every differing byte). diff compares files line by line and shows a line-oriented changeset describing insertions, deletions, and modifications — much more useful for text source files, while cmp is well suited to binary files.

How do I just check if two files are identical, without any output?

Use cmp -s file1 file2 and check the exit status: 0 means identical, 1 means they differ, and 2 means an error occurred (e.g. a file doesn't exist). This is ideal for use inside shell scripts and if statements.

Can cmp compare only part of two files?

Yes. Use -i to skip a number of leading bytes in both files, and -n to limit the comparison to a maximum number of bytes — useful for comparing files with different headers but otherwise identical content.

Does cmp tell you how many differences there are?

By default cmp stops and reports only the first difference. Use -l to list every differing byte position and its value in both files, which effectively shows the full extent of the differences.

Cheat sheet

Download a quick-reference cheat sheet for cmp.