How to Use the Linux cmp Command to Compare Two Files Byte by Byte

The Problem with File Verification

If you have two massive text files containing thousands of lines of programming code, and you want to know if they are exactly identical, you can use the classic diff command. The diff command will analyze both files, highlight the exact sentences that are different, and output a beautiful, human-readable report.

However, what if you are a systems administrator trying to verify the integrity of two massive, 10-gigabyte database backup files (which are highly compressed, non-human-readable binary files)? If you try to run the diff command on a 10-gigabyte binary file, the terminal will instantly choke on the unreadable machine code, consume all of your server’s RAM, and crash.

When you do not care what the exact difference is, and you simply need a lightning-fast mathematical “Yes or No” answer to the question “Are these two files mathematically identical at the structural level?”, you must bypass human-readable tools and use the cmp (Compare) command.

Step 1: The Basic Byte Comparison

The cmp command operates at the absolute lowest level of the Linux operating system. It does not read words or sentences; it reads raw bytes of data.

Assume you have two files: backup_A.zip and backup_B.zip.

cmp backup_A.zip backup_B.zip

The command executes with incredible speed. It lines both files up side-by-side in the computer’s memory and scans them byte by byte.

  • If the files are mathematically identical: The command will output absolutely nothing. It will simply return you to a blank terminal prompt. (In Linux philosophy, silence means success).
  • If the files are different: The moment it detects a single altered byte, it instantly stops scanning and outputs a highly specific technical error: “backup_A.zip backup_B.zip differ: byte 4592, line 15”.

This tells you that the files are essentially identical until it hit the 4,592nd byte of data, where a discrepancy occurred, proving the backup is corrupted.

Step 2: Suppressing Output for Scripts

If you are writing an automated bash script that verifies hundreds of backups every night, you do not want the cmp command printing text to the screen. You just want the script to silently make a decision.

You can use the -s (silent/status) flag.

cmp -s backup_A.zip backup_B.zip

This suppresses all text output. Instead, it relies entirely on hidden “Exit Codes.” It returns a 0 if the files match, and a 1 if they do not match, allowing your script’s if/else statements to route the data accordingly without cluttering the screen.

Step 3: Finding Every Single Difference

By default, cmp is a fail-fast tool. The exact millisecond it finds one mistake, it terminates the scan to save time.

If you are a forensic data analyst, you might need to know the exact location of every single corrupted byte in the entire 10-gigabyte file.

To force the command to scan the entire file and generate a massive list of every single discrepancy, use the -l (verbose/list) flag.

cmp -l backup_A.zip backup_B.zip

The terminal will output a massive three-column table. The first column is the byte number, the second column is the octal value of the byte in the first file, and the third column is the octal value of the byte in the second file. This raw data is useless for a normal user, but it is the holy grail for a cybersecurity researcher trying to determine if a hacker systematically injected a virus into an executable file.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.