On Unix-like operating systems, the cmp command is used to compare two files byte by byte. If a difference is found, it reports the byte and line number where the first difference is found. If no differences are found, by default, cmp returns no output.

This page covers the GNU/Linux version of cmp.

Syntax

cmp [OPTION]… FILE1 [FILE2 [SKIP1 [SKIP2]]]

Options

The optional SKIP1 and SKIP2 specify the number of bytes to skip at the beginning of each file (zero by default).

  • Syntax
  • Options
  • Examples
  • Related commands
  • Linux commands help

SKIP values may be followed by the following multiplicative suffixes:

…and so on for T, P, E, Z, Y.

If a FILE is specified as ‘-’ or not specified, data is read from standard input. cmp’s exit status is 0 if inputs are the same, 1 if different, or 2 if the program encounters a problem.

Additional options:

Examples

cmp file1.txt file2.txt

Compares file1 to file2, reading each file byte-by-byte and comparing them until one of the byte pairs is not equal. When a difference is found, it outputs the location in the file where the difference was found, and exit. Example output:

file.txt file2.txt differ: char 1011, line 112

comm — Compare two sorted files line by line.diff — Identify the differences between two files.