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 date Command: Display and Format the Current Time

Linux date Command: Display and Format the Current Time

Learn how to display and Format the Current Time with the Linux date command, including practical examples, key options, and important precautions.

Linux umount Command: Unmount a Filesystem Safely

Linux umount Command: Unmount a Filesystem Safely

Learn how to unmount a Filesystem Safely with the Linux umount command, including practical examples, key options, and important precautions.

Linux pkill Command: Signal Processes by Name

Linux pkill Command: Signal Processes by Name

Learn how to signal Processes by Name with the Linux pkill command, including practical examples, key options, and important precautions.

Linux time Command: Measure Command Execution Time

Linux time Command: Measure Command Execution Time

Learn how to use the Linux time command to measure elapsed, user CPU, and system CPU time for a command, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux grep Command: Search Text and Filter Command Output

Linux grep Command: Search Text and Filter Command Output

Learn how to use the Linux grep command to search files for text or regular expressions and filter command output, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux gunzip Command: Decompress gzip Files

Linux gunzip Command: Decompress gzip Files

Learn how to decompress gzip Files with the Linux gunzip command, including practical examples, key options, and important precautions.

Linux mount Command: Inspect and Mount Filesystems

Linux mount Command: Inspect and Mount Filesystems

Learn how to inspect and Mount Filesystems with the Linux mount command, including practical examples, key options, and important precautions.

Linux tac Command: Print Lines in Reverse Order

Linux tac Command: Print Lines in Reverse Order

Learn how Linux tac prints records from last to first, handles multiple files and custom separators, and helps inspect recent log entries in reverse order.

Linux Tutorial / Navigate Directories with pwd, ls, cd, and tree

Linux Tutorial / Navigate Directories with pwd, ls, cd, and tree

Use pwd, ls, cd, and tree to find your current location, inspect directories, navigate paths, and check where each command takes you.

Linux Tutorial / Linux Terminal and Shell Basics: Command Syntax and Finding Help

Linux Tutorial / Linux Terminal and Shell Basics: Command Syntax and Finding Help

Learn the difference between a terminal and a shell, read command syntax and output, and use Bash help tools through a short hands-on exercise.