Перейти к содержимому
Оригинал
Kondasamy Jayaraman · Engineering Blog·· 3 дня назадОценка ИИ20

TIL: побайтовое сравнение файлов в Shell-скриптах с помощью cmp

Оригинальный заголовок: TIL: cmp: Fast Byte-by-Byte File Comparison for Shell Scripts

Краткий обзор ИИ

Чтобы проверить, одинаковы ли два файла, в Shell-скрипте можно использовать cmp вместо diff: cmp сравнивает файлы побайтово и останавливается на первом несовпадении — даже для файлов размером в гигабайты это займёт миллисекунды, тогда как diff построчно разбирает весь текст. Тихий режим -s возвращает результат через код выхода: 0 — файлы совпадают, 1 — различаются, 2 — файл отсутствует или недоступен для чтения. cmp подходит для бинарных файлов и автоматизированных скриптов, а в POSIX-окружениях он есть по умолчанию.

Полный текст

Полный текст на выбранном языке ожидает перевода. Пока показан оригинал.

October 4, 2026 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.

Источник: Kondasamy Jayaraman · Engineering Blog · kondasamy.com