cmp
Compare two files byte by byte
TLDR
Output char and line number of the first difference between two files
Output info of the first difference: char, line number, bytes, and values
Output the byte numbers and values of every difference
Compare files but output nothing, yield only the exit status
SYNOPSIS
cmp [OPTION]... FILE1 [FILE2]
PARAMETERS
-b, --print-bytes
Print differing bytes as characters, ignoring -l.
-i SKIP, --ignore-initial=SKIP
Skip first SKIP bytes of both inputs.
-i SKIP1:SKIP2
Skip first SKIP1 bytes of FILE1 and SKIP2 of FILE2.
-l, --verbose
Output offsets and octal values for all differing bytes.
-n LIMIT, --bytes=LIMIT
Compare at most LIMIT bytes.
-s, --quiet, --silent
Suppress all output; use exit status only.
--help
Display usage summary and exit.
--version
Output version information and exit.
DESCRIPTION
cmp is a Unix utility that compares two files byte by byte, identifying the first position where they differ. Ideal for binary files, it reports the offset (1-based) of the mismatch and the differing bytes, printing printable characters as-is or using octal escapes for non-printable ones. If no differences are found, it exits silently with status 0. A difference yields status 1, while errors (e.g., missing files) result in status 2.
Unlike diff, which is line-oriented and suited for text, cmp performs exact binary comparison, making it faster for detecting any change. It supports comparing a file with stdin (using - for FILE2) and handles regular files, but not directories. Output is minimal by default, focusing on the first mismatch to enable scripting and automation.
Common use cases include verifying backups, checking file integrity, or integrating into build systems to detect changes. For verbose output showing all differences, use -l. GNU coreutils version adds options like byte limits (-n) for large files, improving efficiency.
CAVEATS
cmp stops at the first difference by default (use -l for all); treats files as binary, so text line endings may confuse interpretation; inefficient for very large identical files without -n.
EXIT STATUS
0: files identical
1: files differ
2: error (e.g., I/O failure, missing file).
BEHAVIOR NOTES
FILE1 required; FILE2 defaults to stdin if omitted or -; offsets are 1-based decimal.
HISTORY
Originated in Version 7 Unix (1979); POSIX.1-2001 standardized core options; GNU coreutils enhanced with -i SKIP1:SKIP2, -n since 1990s for better binary handling.


