Skip to content
On this page

    cmp: Fast Byte-by-Byte File Comparison for Shell Scripts

    2 min read

    Today I stopped using diff to check whether two files match in shell scripts.

    My old default check was diff -q file1 file2 > /dev/null. That command solves a different problem. diff parses text line by line to calculate edit hunks. On large disk images, database dumps, or compiled binaries, diff reads entire files and consumes unnecessary CPU cycles.

    For a boolean equality check, cmp reads byte by byte and halts at the first mismatch.

    The Basic Command

    Running cmp without flags prints the first point of divergence:

    cmp config-old.json config-new.json

    If the files match, cmp produces no output. If they differ, it pinpoints the exact byte offset and line:

    config-old.json config-new.json differ: byte 25, line 3

    Because cmp halts at the first mismatch, it finishes in milliseconds even on gigabyte-sized files.

    The Shell Script Pattern

    The -s (silent) flag suppresses standard output and error streams. The command exits with three status codes for shell conditions:

    • 0: Files are identical
    • 1: Files differ
    • 2: Trouble encountered (file missing or unreadable)
    if cmp -s config.new /etc/app/config.json; then
      echo "Configuration unchanged. Skipping reload."
    else
      echo "Configuration changed. Applying update..."
      cp config.new /etc/app/config.json
      systemctl restart app
    fi

    The if block checks the exit code. You drop boilerplate redirects like > /dev/null 2>&1.

    cmp vs diff

    CommandComparison MethodHalts AtBest For
    cmpByte by byteFirst mismatchEquality checks, binary files, shell automation
    diffLine by lineEnd of fileReviewing human-readable text edits

    Quick Reference

    FlagPurposeExample
    -sRun silent and return an exit code without outputcmp -s file1 file2
    -lPrint byte offset and differing octal valuescmp -l file1 file2
    -bPrint differing bytes as readable characterscmp -b file1 file2
    -n NCompare at most N bytescmp -n 1024 disk1.img disk2.img

    Common Workflows

    • Backup validation: Verify that a restored database dump matches the original byte for byte:
      cmp -s db-backup.sql /mnt/storage/db-backup.sql
    • Binary artifact checks: Verify that firmware images or compiled binaries match upstream builds:
      cmp app-linux-x64 dist/app-linux-x64
    • Build pipeline triggers: Prevent downstream rebuild steps by checking if generated asset files changed before writing them.

    What I Learned

    • cmp halts at the first mismatch, leaving the rest of the file unread.
    • The -s flag turns cmp into a boolean test for shell scripts.
    • The command compares binary assets where diff issues warnings or chokes on null bytes.
    • Exit code 2 flags missing or inaccessible files, separating system errors from content diffs.
    • POSIX environments include cmp by default, requiring no third-party package.

    Have a favorite Unix command that simplified your automation scripts? Reach out on LinkedIn.