Binary and Low-Level File Comparisons in Linux
When verifying software builds, checking data backups, verifying disk image integrity, or determining whether two configuration files are strictly identical, administrators need a rapid verification method. While text-based comparison tools like diff or comm excel at parsing ASCII and UTF-8 prose line by line, they struggle when handling compiled binary executables, database dumps, audio tracks, or disk image blobs. Furthermore, diff reads entire files into system memory to calculate complex edit distances. Linux provides the lightweight, POSIX-compliant cmp utility, part of GNU Diffutils, specifically designed to compare two files byte by byte and report the exact byte offset and line number of the very first discrepancy.
Basic Syntax and Default Behaviour
The standard command syntax for cmp is straightforward:
cmp [options] file1 file2 [skip1 [skip2]]
When you run cmp on two files without any extra flags:
cmp firmware_v1.bin firmware_v2.bin
- If the files are strictly identical:
cmpproduces absolutely no terminal output and immediately exits with a success return code of0. - If the files differ:
cmpstops evaluating at the first mismatch and outputs a concise alert:firmware_v1.bin firmware_v2.bin differ: byte 1042, line 14
Because cmp stops scanning the instant a single non-matching byte is identified, comparing massive multi-gigabyte files completes in milliseconds if differences occur early in the byte stream.
Listing All Differing Bytes with -l
By default, cmp halts after the first discrepancy. If you need a comprehensive audit of every non-matching byte across two binary structures, use the -l (verbose / list) flag:
cmp -l binary_orig.dat binary_mod.dat
The resulting terminal table outputs three numerical columns:
- Byte Offset: The 1-based decimal byte position where the difference occurred.
- File 1 Value: The octal value of the byte in the first file.
- File 2 Value: The octal value of the byte in the second file.
To print actual readable character representations alongside octal byte values, combine -l with the -b flag:
cmp -b -l file1.txt file2.txt
Silent Execution in Shell Scripts with -s
In shell automation scripts and continuous integration pipelines, printing visual error messages to standard output is often undesirable; you simply need a clean exit status to control conditional branching.
Use the -s (silent / quiet) flag to suppress all terminal output:
cmp -s image_master.iso image_copy.iso
if [ $? -eq 0 ]; then
echo "Verification successful: Files are byte-for-byte identical."
else
echo "Integrity check failed: Corruption or differences detected."
fi
Return code standards for cmp:
0: Files are identical.1: Files differ.2: An error occurred (e.g. file not found or permission denied).
Skipping Header Offsets and Limiting Comparison Length
When analyzing file formats that include variable headers (such as embedded container timestamps, metadata blocks, or archive envelopes) followed by identical payloads, you can instruct cmp to skip a fixed number of initial bytes:
cmp file1.dat file2.dat 128 128
This skips the first 128 bytes of both files before initiating byte comparison.
To restrict the comparison to a maximum number of bytes rather than scanning the entire file, append the -n parameter:
cmp -n 1024 header1.bin header2.bin
This evaluates only the first 1024 bytes (1 KB) of each file.
Comparing cmp vs diff vs md5sum
Choosing the correct Linux comparison utility depends on your data type:
cmp: The fastest tool for binary files and raw data streams; halts immediately at the first divergent byte with minimal memory usage.diff: Best suited for human-readable source code and text documents where you need structural context, additions, and deletions.md5sum/sha256sum: Ideal for verifying file integrity across networks or where comparing files simultaneously in local memory is impractical.