Back to the command reference

cmp

FilesFundamentals In simulator In real Linux

Compare two files byte by byte

Name: cmp = compare

You can practise it right away in the simulator, and use it in real Linux mode too.

Description

cmp compares two files byte by byte and reports where the first difference is (byte and line). It suits binary files; for text, diff is more informative. -s prints nothing and only sets the exit status.

Syntax

cmp [OPTION]... FILE1 FILE2

Common options

-s / --quiet / --silentSilent; only the exit status tells
-l / --verboseList every differing byte
-b / --print-bytesAlso show the differing bytes as characters
-n / --bytes LIMITCompare at most LIMIT bytes
-i / --ignore-initial SKIPSkip the first SKIP bytes

Examples

  • $ cmp a.bin b.bin

    Show the first difference

    Explain
  • $ cmp -s a.bin b.bin && echo same

    Print same if identical

    Explain

Related