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

More in This Category
Linux last Command: Review Login and Reboot History

Linux last Command: Review Login and Reboot History

Learn how to use the Linux last command to review user login sessions, system reboots, and shutdown history, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux curl Command: Make URL Requests and Inspect HTTP Responses

Linux curl Command: Make URL Requests and Inspect HTTP Responses

Learn how to make URL Requests and Inspect HTTP Responses with the Linux curl command, including practical examples, key options, and important precautions.

Linux renice Command: Change the Priority of a Running Process

Linux renice Command: Change the Priority of a Running Process

Learn how to change the Priority of a Running Process with the Linux renice command, including practical examples, key options, and important precautions.

Linux modinfo Command: Inspect Kernel Module Details

Linux modinfo Command: Inspect Kernel Module Details

Learn how to inspect Kernel Module Details with the Linux modinfo command, including practical examples, key options, and important precautions.

Linux tr Command: Translate and Delete Characters

Linux tr Command: Translate and Delete Characters

Learn how to translate and Delete Characters with the Linux tr command, including practical examples, key options, and important precautions.

Linux whatis Command: Show One-Line Command Descriptions

Linux whatis Command: Show One-Line Command Descriptions

Learn how to use the Linux whatis command to display concise descriptions of commands and manual pages, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux scp Command: Copy Files over SSH

Linux scp Command: Copy Files over SSH

Learn how to copy Files over SSH with the Linux scp command, including practical examples, key options, and important precautions.

Best Linux Distributions for Web Servers: Ubuntu, Debian, or Rocky Linux?

Best Linux Distributions for Web Servers: Ubuntu, Debian, or Rocky Linux?

Compare Ubuntu Server, Debian, and Rocky Linux for web hosting based on support lifecycle, package freshness, documentation, operational stability, and application requirements.

Linux tar Command: Create and Extract Archives

Linux tar Command: Create and Extract Archives

Learn how to create and Extract Archives with the Linux tar command, including practical examples, key options, and important precautions.

Linux whoami Command: Show the Effective User Name

Linux whoami Command: Show the Effective User Name

Learn how to show the Effective User Name with the Linux whoami command, including practical examples, key options, and important precautions.