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 [SKIP1 [SKIP2]]]

PARAMETERS

-b, --print-bytes
    Print differing bytes. Print control characters as ^C and other non-printing bytes as M-X.

-i SKIP1, --ignore-initial=SKIP1
    Skip SKIP1 bytes from the beginning of FILE1.

-j SKIP1, --jump-initial=SKIP1
    Same as --ignore-initial.

-i SKIP1:SKIP2, --ignore-initial=SKIP1:SKIP2
    Skip SKIP1 bytes from the beginning of FILE1 and SKIP2 bytes from the beginning of FILE2.

-j SKIP1:SKIP2, --jump-initial=SKIP1:SKIP2
    Same as --ignore-initial.

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

-n LIMIT, --bytes=LIMIT
    Compare at most LIMIT bytes.

-s, --quiet, --silent
    Suppress all normal output; return exit status only.

-v, --version
    Output version information and exit.

--help
    Display help message and exit.

DESCRIPTION

The cmp command in Linux is a utility used to compare two files. It meticulously compares the files byte by byte and identifies the first byte where they differ. cmp is useful for verifying file integrity after copying or transferring data, ensuring data consistency, and pinpointing discrepancies between similar files. By default, the command outputs the byte and line number where the first difference is detected, and then terminates. However, it can be instructed to display all differing bytes, display no output if files are identical, or handle end-of-file (EOF) conditions differently.
cmp is a foundational command for data verification and debugging file-related issues.

CAVEATS

cmp considers identical files to be 'different' if they have different file lengths, and exits with a specific status code to indicate this.

EXIT STATUS

The cmp command can return one of the following exit status:
0 if files are identical.
1 if files are different.
2 if an error occurred.

HANDLING EOF

When one file is shorter than the other, cmp will report end-of-file on the shorter file. The exit status will be 1.

HISTORY

The cmp command has been a standard Unix utility for a long time, present in early versions of Unix. Its purpose remains unchanged: to offer a low-level, byte-by-byte comparison of files.

SEE ALSO

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

Copied to clipboard