cmp Command in Linux: Compare Files Byte by Byte (Examples & Reference)

Deepak Prasad
Tested on Ubuntu 25.04 (Plucky Puffin)
Package cmp (GNU diffutils) 3.10
Applies to Ubuntu, Debian, RHEL, Rocky Linux, AlmaLinux, Oracle Linux, CentOS Stream, Fedora, Arch Linux, SUSE, openSUSE, Alpine
Privilege read-only (no elevated privileges)
Man page cmp(1)
Scope cmp compares two files byte by byte on Linux. It reports the first difference, supports silent exit codes for scripts, and can list every differing byte — unlike line-oriented diff.
Related guides Linux commands

cmp — quick reference

Basic comparison

When to use Command
Compare two files from the start cmp file1 file2
Suppress output; use exit status only cmp -s file1 file2
Show differing byte values at first mismatch cmp -b file1 file2
List every differing byte offset cmp -l file1 file2

Skip and limit

When to use Command
Skip the first N bytes in both files cmp -i N file1 file2
Skip different counts per file cmp -i SKIP1:SKIP2 file1 file2
Compare at most N bytes cmp -n N file1 file2
Positional skip (legacy syntax) cmp file1 file2 SKIP1 SKIP2

Help and version

When to use Command
Show built-in usage text cmp --help
Show diffutils version cmp --version

cmp — command syntax

Synopsis from cmp --help on Ubuntu 25.04 (GNU diffutils 3.10):

text
Usage: cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]

cmp reads bytes from two inputs and stops at the first difference unless you pass -l (verbose list). Exit status 0 means identical, 1 means different, 2 means error.


cmp — command examples

Essential Identical files produce no output

When two files match byte for byte, cmp prints nothing and exits 0 — ideal for scripts.

Prepare two matching files:

bash
echo 'same content' > /tmp/cmp-same1.txt
echo 'same content' > /tmp/cmp-same2.txt
cmp /tmp/cmp-same1.txt /tmp/cmp-same2.txt
echo exit:$?

Sample output:

output
exit:0

Silence means success. Check $? in scripts when you use cmp -s.

Essential First byte difference (default behavior)

Without options, cmp stops at the first mismatch and prints byte and line numbers.

Prepare differing files:

bash
echo 'line one' > /tmp/cmp-a.txt
echo 'line two' > /tmp/cmp-b.txt
cmp /tmp/cmp-a.txt /tmp/cmp-b.txt
echo exit:$?

Sample output:

output
/tmp/cmp-a.txt /tmp/cmp-b.txt differ: byte 6, line 1
exit:1

Byte 6 is where o versus t diverges in one vs two.

Essential Show the differing bytes with -b

-b adds octal values and printable characters for the mismatch.

Run the command:

bash
cmp -b /tmp/cmp-a.txt /tmp/cmp-b.txt

Sample output:

output
/tmp/cmp-a.txt /tmp/cmp-b.txt differ: byte 6, line 1 is 157 o 164 t

157 and 164 are octal byte values; o and t are the visible characters.

Common Silent check for scripts (-s)

-s suppresses normal output. Scripts branch on the exit code only.

Run the commands:

bash
cmp -s /tmp/cmp-a.txt /tmp/cmp-b.txt
echo different:$?
cmp -s /tmp/cmp-same1.txt /tmp/cmp-same2.txt
echo same:$?

Sample output:

output
different:1
same:0

Use if cmp -s file1 file2; then … instead of parsing text output.

Common List every differing byte with -l

When files diverge in many places, -l prints offset and both byte values for each difference.

Run the command:

bash
cmp -l /tmp/cmp-a.txt /tmp/cmp-b.txt | head -6

Sample output:

output
6 157 164
7 156 167
8 145 157
9 40 40

Each line is: position, byte in file1, byte in file2 (octal). Large binaries can produce long lists — pipe to head while exploring.

Common Skip headers with -i

Compare payloads after a common header or after aligning mismatched offsets.

Skip one byte in both files:

bash
cmp -i 1 /tmp/cmp-a.txt /tmp/cmp-b.txt
echo exit:$?

Sample output:

output
/tmp/cmp-a.txt /tmp/cmp-b.txt differ: byte 5, line 1
exit:1

Different skip per file:

bash
cmp -i 1:2 /tmp/cmp-a.txt /tmp/cmp-b.txt

Positional form (same idea):

bash
cmp /tmp/cmp-a.txt /tmp/cmp-b.txt 1
Common Compare only the first N bytes (-n)

Validate a magic header or fixed-size record without reading entire multi-gigabyte files.

Run the command:

bash
cmp -n 4 /tmp/cmp-a.txt /tmp/cmp-b.txt
echo exit:$?

Sample output:

output
exit:0

The first four bytes (line) match even though the full files differ.

Advanced Compare binary files byte by byte

cmp does not treat input as text — NUL bytes and binary data are fine.

Create two nearly identical binary blobs:

bash
printf 'hello\x00world' > /tmp/cmp-bin1
printf 'hello\x01world' > /tmp/cmp-bin2
cmp -b /tmp/cmp-bin1 /tmp/cmp-bin2

Sample output:

output
/tmp/cmp-bin1 /tmp/cmp-bin2 differ: byte 6

Use cmp for firmware images, checksum spot-checks, and backup verification; use diff when you need readable text deltas.

Advanced Remove test files when finished

Lab examples under /tmp should not linger on shared hosts.

Run the command:

bash
rm -f /tmp/cmp-a.txt /tmp/cmp-b.txt /tmp/cmp-same1.txt /tmp/cmp-same2.txt /tmp/cmp-bin1 /tmp/cmp-bin2
ls /tmp/cmp-*.txt 2>&1 || echo 'cmp test files removed'

Sample output:

output
ls: cannot access '/tmp/cmp-*.txt': No such file or directory
cmp test files removed

cmp — when to use / when not

Use cmp when Use something else when
  • You only need to know whether two files are identical
  • You want the first byte offset of a mismatch
  • You are comparing binary files or backups
  • Scripts should branch on exit status (cmp -s)
  • You need line-by-line text deltas → diff
  • You want unified patches for source trees → diff -u
  • You compare directory trees → diff -r
  • You need cryptographic integrity → sha256sum

cmp vs diff

cmp diff
Unit Byte Line (text)
Output when same Silent Silent
Typical use Binary equality, CI guards Source code review
All differences cmp -l diff full output

Both ship in diffutils on Ubuntu.


cmp — interview corner

What does cmp do?

cmp compares two files byte by byte. If they are identical it prints nothing and exits 0. If they differ it reports the first byte offset (and line number for text files).

It is simpler and faster than diff when you only need a yes/no answer.

A strong answer is:

"cmp compares files byte by byte — silent on match, first offset on mismatch. I use it for binary equality checks and script exit codes."

What exit codes does cmp return?

0 — files are identical. 1 — files differ. 2 — trouble (missing file, permission error, invalid option).

Scripts should use cmp -s and test $? rather than parsing stdout.

A strong answer is:

"Exit 0 same, 1 different, 2 error. In bash I use cmp -s and check $? for automation."

When do you choose cmp over diff?

Choose cmp for binary files, backup verification, or when you only care if files are equal. diff is for human-readable text changes between source files.

A strong answer is:

"cmp for binary or equality checks; diff when I need line-based patches for text."

What does cmp -s do?

-s (silent) suppresses the normal difference message. You rely entirely on the exit status — the usual pattern in CI and shell scripts.

A strong answer is:

"cmp -s runs quietly — I branch on exit code 0 or 1 without parsing output."

How do you compare part of a file with cmp?

Use -i to skip leading bytes (one count for both files or SKIP1:SKIP2 per file). Use -n to cap how many bytes are compared after skips.

Legacy syntax passes skip values as optional third and fourth arguments: cmp file1 file2 SKIP1 SKIP2.

A strong answer is:

"cmp -i skips headers, cmp -n limits length — useful for magic bytes or records without hashing whole files."


Troubleshooting

Symptom Likely cause Fix
Exit 2 Missing path or permission Check files exist; ls -l
cmp: EOF on file1 One file shorter Expected — shorter file ends first
No output but files "look" different Text editor hides CRLF vs LF cmp -l or od -c
Huge -l output Large binary diff Use -n or checksums
Unknown option Non-GNU cmp cmp --help on the host

Rohan Timalsina

is a technical writer and Linux enthusiast who writes practical guides on Linux commands and system administration. He focuses on simplifying complex topics through clear explanations.

  • Linux
  • HTML5
  • JavaScript
  • Web Design
  • Front-end Web Development