LinuxCommandLibrary

cmp

Compare two files byte by byte

TLDR

Output char and line number of the first difference between two files

$ cmp [path/to/file1] [path/to/file2]
copy

Output info of the first difference: char, line number, bytes, and values
$ cmp [[-b|--print-bytes]] [path/to/file1] [path/to/file2]
copy

Output the byte numbers and values of every difference
$ cmp [[-l|--verbose]] [path/to/file1] [path/to/file2]
copy

Compare files but output nothing, yield only the exit status
$ cmp [[-s|--quiet]] [path/to/file1] [path/to/file2]
copy

SYNOPSIS

cmp [OPTION]... FILE1 FILE2

PARAMETERS

-s, --quiet, --silent
    Suppress all normal output. cmp only returns an exit status.

-l, --verbose
    Output the byte number (decimal) and the differing byte values (octal) for all differences.

-b, --print-bytes
    Print the differing bytes themselves (in octal) in addition to the offset for the -l option. This option is often implied by -l or explicitly useful for clearer output alongside -l.

-i SKIP1:SKIP2, --ignore-initial=SKIP1:SKIP2
    Skip the first SKIP1 bytes of FILE1 and SKIP2 bytes of FILE2. If SKIP2 is omitted, it defaults to SKIP1.

--help
    Display a help message and exit.

--version
    Output version information and exit.

DESCRIPTION

cmp is a command-line utility used to compare two files byte by byte. It is primarily designed to report if two files are identical or, if they differ, to indicate the location of the first differing byte and line number. Unlike diff, which is optimized for textual differences, cmp performs a raw, byte-level comparison, making it suitable for checking the integrity of binary files, comparing images, or verifying exact duplicates. Its primary utility lies in scripting, where its exit status (0 for identical, 1 for differences, 2 for errors) can be used to control program flow. By default, cmp produces no output if the files are identical; it only reports discrepancies.

CAVEATS

cmp performs a strict byte-by-byte comparison. This means it will report differences even for minor changes like different newline characters (e.g., LF vs CRLF) that might be visually insignificant in text files. For sophisticated textual comparison and patch generation, diff is a more appropriate tool. cmp's simplicity is its strength for binary files or quick checks of exact equality.

EXIT STATUS

cmp uses specific exit status codes to indicate the result of the comparison, which is crucial for scripting:
0: The files are identical.
1: The files are different.
2: An error occurred (e.g., inaccessible file, invalid option).

HISTORY

The cmp command is a fundamental utility in the Unix operating system, dating back to its early versions. It has been a standard part of the GNU Core Utilities, demonstrating its enduring utility for basic file comparison tasks. Its design prioritizes simplicity and efficiency, focusing solely on byte-level equality, a characteristic that has remained consistent throughout its development.

SEE ALSO

diff(1), comm(1), diff3(1), sdiff(1), md5sum(1), sha256sum(1)

Copied to clipboard