Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

cmp - compare two files

&pagelevel(4)&pagelevel

The cmp command does a comparison of two files byte by byte (character by character). If the files differ, cmp reports the differences on standard output.

If the files are identical, cmp remains silent.


Syntax


cmp[  -l|-s] file1 file2

No option specified

If the files are identical, cmp remains silent.

If the files differ, cmp indicates the byte (character) and line number of the first difference that it detects between file1 and file2 as shown below:

file1 file2 differ: char bytenumber, line linenumber

-l

All differences are reported in the following form:

bytenumber byte(file1) byte(file2)

bytenumber represents the displacement of the difference from the beginning of the file. The first byte of the file is assigned number 1, and blanks are counted.
bytenumber is given in decimal notation.

The byte columns show the bytes which differ between file1 and file2 and are in octal notation. An ASCII table of octal-coded values is provided in Tables and directories. If the files are identical, nothing is output.

-s

cmp remains silent. The exit status value is returned but not automatically displayed on the screen. You can enter the command echo $? to query the exit status.

file1 file2

The names of the files that you wish to compare.

  • If you use a dash - as the name for file1, cmp reads from standard input and compares your input with file2.

  • If one of the two files ends before cmp can detect a difference, cmp reports that the end of the file has been reached in the shorter file by issuing the following message:

    cmp: EOF on file

  • If one character is missing in one of two otherwise identical files, cmp -l reports all following bytes as differences. This is due to the shift in character positions.

  • The first character of a file is assigned bytenumber 1, not 0.

  • Blanks and newline characters are included in the bytenumber count.

Exit status

0files identical
1files differ

>1

inaccessible file or missing argument

Error

cmp: cannot open file

You do not have read permission for one of the files, or one of the files does not exist.

Locale

The following environment variables affect the execution of cmp:

LANG

Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.

LC_ALL

If set to a non-empty string value, override the values of all the other internationalization variables.

LC_CTYPE

Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments). LC_CTYPE governs character classes, character conversion (shifting) and the behavior of character classes in regular expressions.

LC_MESSAGES

Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

NLSPATH

Determine the location of message catalogs for the processing of LC_MESSAGES.

Example 1

Compare two files and print the differing bytes (octal) and their bytenumber (decimal).

$ echo 1 2 3 4 5 7 8 a >file1
$ echo 1 2 3 4 5 6 9 a >file2
$ cmp -l file1 file2
    11 367 366
    13 370 371
$

Example 2

The shell script delete.eq compares two files and deletes one of them if they are identical.

if cmp -s $1 $2
then
  rm $2
fi


When you call the script with

$ delete.eq file1 file2

you pass file1 and file2 to it as positional operands. The -s option causes cmp to return the exit status. If the value of the exit status is 0 (=true), file2 is deleted; otherwise, it is retained.

See also

comm, diff