cmp Command Explained

cmp Command Explained

cmp compares two files at the level of raw bytes, which makes it the right tool exactly where diff is the wrong one: binary files, where there is no meaningful concept of a “line” to compare.

Basic usage

cmp file1.bin file2.bin
# file1.bin file2.bin differ: byte 145, line 4

cmp reports the first point of difference it finds, given as both a byte offset and a line number (line number is really only meaningful for text-like content, but cmp reports it regardless of file type). If the two files are completely identical, cmp produces no output whatsoever and simply exits successfully.

cmp identical1.txt identical2.txt
# (no output at all, meaning they are exactly the same)
echo $?
# 0

Why cmp exists alongside diff

diff photo1.jpg photo2.jpg
# Binary files photo1.jpg and photo2.jpg differ
# (diff can tell you THAT they differ, but gives essentially
#  no useful detail about a binary file's actual content)

cmp photo1.jpg photo2.jpg
# photo1.jpg photo2.jpg differ: byte 2048, line 9

diff is fundamentally built around comparing text, line by line, and gives only a generic “binary files differ” message when pointed at non-text content. cmp operates purely at the byte level regardless of content type, making it the correct tool for comparing compiled executables, images, compressed archives, or any other binary format where “lines” are not a meaningful concept.

Seeing every differing byte, not just the first

cmp -l file1.bin file2.bin
# 145  61  62
# 146  144 145
# 312  17  20

By default, cmp stops after reporting the very first difference. -l (list) instead continues through the entire file, reporting every single differing byte position along with the two differing values (shown in octal by default) from each file. This is far more output, but useful when you need a complete picture of how extensively two files diverge rather than just confirming that a difference exists somewhere.

Silent mode for scripting

if cmp -s file1.bin file2.bin; then
  echo "Files are identical"
else
  echo "Files differ"
fi

-s (silent) suppresses all output and communicates the result purely through the exit code: 0 for identical, 1 for different, 2 if something went wrong, such as one of the files not existing at all. This is the standard, efficient pattern for checking file equality directly inside a script, without generating or discarding output you do not actually need to see.

cmp vs checksums

sha256sum file1.bin file2.bin
# a1b2c3...  file1.bin
# d4e5f6...  file2.bin
# (confirms they differ, but tells you NOTHING about where)

cmp file1.bin file2.bin
# file1.bin file2.bin differ: byte 2048, line 9
# (tells you exactly where the first difference is located)

Checksum tools like sha256sum are efficient for confirming whether two files match, especially when verifying a download against a known, previously published hash. But if a checksum reveals a mismatch, it gives you no information at all about where or how the files actually differ. cmp (or diff, for text) is the tool to reach for once you need to actually investigate the nature of a discrepancy rather than merely detect its existence.

Comparing files of different lengths

cmp short.bin long.bin
# cmp: EOF on short.bin after byte 512, line 3

If every byte matches up through the length of the shorter file, cmp reports that it reached the end of the shorter file without finding any actual byte-level difference, effectively telling you that one file appears to be a truncated (or the other, an extended) version of the other, rather than reporting a genuine content mismatch.

Skipping a header before comparing

cmp -i 512 file1.bin file2.bin

-i (ignore initial bytes) skips a specified number of bytes at the start of both files before beginning the comparison, useful for files with a fixed-size header (a common structure in many binary formats) that is expected to differ or is irrelevant to the comparison you actually care about.

Frequently Asked Questions

What does the cmp command do?

cmp compares two files byte by byte and reports the location (byte offset and line number) of the first place they differ. If the files are identical, cmp produces no output at all and exits successfully. It works on any file type, including binary files, unlike diff, which is designed specifically for comparing text.

What is the difference between cmp and diff?

diff is designed for text files and shows a detailed, line-oriented summary of everything that changed, added lines, removed lines, modified lines, all clearly labeled. cmp works at the raw byte level and, in its default mode, only reports the first point of difference before stopping, without describing the nature of the change or continuing to report further differences. cmp is the appropriate tool for binary files (executables, images, archives) where line-based comparison does not make conceptual sense.

How do I see every byte that differs between two files, not just the first one?

Add the -l (lowercase L) flag, such as cmp -l file1.bin file2.bin, which lists every differing byte position along with the differing byte values from each file, in both cases shown in octal by default, rather than stopping after reporting only the first difference.

How do I quickly check if two files are identical without seeing any details at all?

Use the -s flag (silent), such as cmp -s file1.bin file2.bin, which produces no output at all and communicates the result purely through its exit code: 0 if the files are identical, 1 if they differ, and 2 if an error occurred, such as one of the files not existing. This is the standard pattern for checking file equality inside a script.

Why would I use cmp instead of checksums like md5sum or sha256sum to check if two files match?

Checksums are efficient for confirming whether two files match or for verifying a file against a known, previously recorded hash, but if they do not match, a checksum alone tells you nothing about where or how the files differ. cmp (or diff for text) tells you exactly where the difference is located, which is useful when you specifically need to investigate or explain a mismatch rather than just detect that one exists.

Can cmp compare files of different sizes?

Yes, cmp compares byte by byte up to the length of the shorter file. If every byte matches up to that point, cmp reports that one file appears to be a truncated or extended version of the other, specifically noting that end-of-file was reached on the shorter file before any actual byte-level difference was found.