Linux cmp command

Updated: 11/06/2021 by Computer Hope
cmp command

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.


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


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

SKIP values may be followed by the following multiplicative suffixes:

kB kilobytes 1000
K kibibytes 1024
MB megabytes 1,000,000
M mebibytes 1,048,576
GB gigabytes 1,000,000,000
G gibibytes 1,073,741,824

...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:

-b, --print-bytes Print differing bytes.
-i, --ignore-initial=SKIP Skip first SKIP bytes of both files.
Skip first SKIP1 bytes of FILE1 and the first SKIP2 bytes of FILE2.
-l, --verbose Output byte numbers and differing byte values.
-n, --bytes=LIMIT Compare at most LIMIT bytes.
-s, --quiet, --silent Suppress all normal output.
--help Display a help message and exit.
-v, --version Output version information and exit.


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.