Linux cmp Command: Compare Files Byte by Byte

The cmp command compares two files byte by byte. By default, it stops at the first mismatch and reports its byte and line position, making it useful for fast equality checks and locating the earliest difference.

This guide explains the basic syntax, key options, practical examples, related commands, common cautions, and frequently asked questions for cmp.

What Is the cmp Command?

Cmp reads both inputs from the beginning and exits silently with status 0 when the compared bytes are identical. A difference normally produces status 1, while an input or execution problem produces status 2. For human-readable line changes in text, diff is usually more informative.

Basic Syntax

cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]

Options and edge cases can vary by implementation and version. Check cmp --help and man cmp on the target system before relying on implementation-specific behavior.

Basic cmp Usage

Compare two files

If the files differ, the default output identifies the first mismatch.

cmp image-a.bin image-b.bin

Check equality silently

Use -s and inspect the exit status in a script.

cmp -s copy.iso original.iso

Compare only an initial range

GNU cmp accepts -n to limit the number of bytes read.

cmp -n 4096 file-a file-b

Read the first different byte position

cmp compares two files byte by byte and tells you the first different position. If the files are identical, it basically does not output anything.

printf 'A' > first.bin
printf 'B' > second.bin
cmp first.bin second.bin

Example output (phrases may vary depending on implementation):

first.bin second.bin differ: byte 1, line 1

Exit status 1 means there is a difference, 2 indicates issues like read errors. If you don't want screen output in a script, use cmp -s and check the exit status.

Key Options

Option Purpose
-s, --silent Suppress normal output and use only the exit status.
-l, --verbose Print the position and values of every differing byte.
-b, --print-bytes Print differing bytes in a displayable form.
-i SKIP Skip an initial number of bytes in both inputs.
-i SKIP1:SKIP2 On GNU systems, use different skip amounts for the inputs.
-n LIMIT Compare no more than LIMIT bytes.

Practical Examples

Branch on equality in a script

Use the exit status instead of parsing locale-dependent diagnostic text.

if cmp -s generated.conf current.conf; then
  printf '%s\n' 'No change'
else
  printf '%s\n' 'Files differ'
fi

Compare a fixed header region

Limit the comparison when a file format has a known header size.

cmp -n 512 disk-a.img disk-b.img

Skip container headers

Use a supported skip form when only the payload after a fixed offset should be compared.

cmp -i 4096 payload-a.bin payload-b.bin

cmp and Related Commands

Command Primary role Best fit
cmp Byte-level first mismatch Equality and offset diagnosis
diff Line-level text changes Readable text comparison
sha256sum Independent file digests Remote or many-file verification
comm Compare sorted line sets Common and unique records

Important Cautions

Verbose output can be enormous

If a large file differs throughout, -l can print a line for every changed byte.

Distinguish difference from trouble

Status 1 means different; status 2 can mean a read error, missing input, or invalid invocation.

Byte equality is not authenticity

Cmp proves the compared ranges match; it does not verify origin, signature, or file-format validity.

FAQ

Does no output mean the files are equal?

If cmp completed successfully with status 0, the compared ranges are byte-for-byte equal.

How do I show every mismatch?

Use cmp -l FILE1 FILE2, but expect large output when differences are numerous.

Can cmp compare text files?

Yes, but diff presents changed lines more readably.

How is cmp different from a checksum?

Cmp reads two inputs together, while checksums produce independent digests that are convenient across machines or times.

Related Resources