The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group


cmp - compare two files


cmp [ -l | -s ] file1 file2


The cmp utility compares two files. The cmp utility will write no output if the files are the same. Under default options, if they differ, it will write to standard output the byte and line number at which the first difference occurred. Bytes and lines will be numbered beginning with 1.


The cmp utility supports the XBD specification, Utility Syntax Guidelines  .

The following options are supported:

(Lower-case ell.) Write the byte number (decimal) and the differing bytes (octal) for each difference.
Write nothing for differing files; return exit status only.


The following operands are supported:
A pathname of the first file to be compared. If file1 is "-", the standard input will be used.
A pathname of the second file to be compared. If file2 is "-", the standard input will be used.

If both file1 and file2 refer to standard input or refer to the same FIFO special, block special or character special file, the results are undefined.


The standard input will be used only if the file1 or file2 operand refers to standard input. See the INPUT FILES section.


The input files can be any file type.


The following environment variables affect the execution of cmp:
Provide a default value for the internationalisation variables that are unset or null. If LANG is unset or null, the corresponding value from the implementation-dependent default locale will be used. If any of the internationalisation variables contains an invalid setting, the utility will behave as if none of the variables had been defined.
If set to a non-empty string value, override the values of all the other internationalisation variables.
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).
Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error and informative messages written to standard output.
Determine the location of message catalogues for the processing of LC_MESSAGES .




In the POSIX locale, results of the comparison will be written to standard output. When no options are used, the format will be:

"%s %s differ: char %d, line %d\n", file1,
<byte number>,

When the -l option is used, the format is:

"%d %o %o\n", <byte number>,
<differing byte>,
<differing byte>
for each byte that differs. The first <differing byte> number is from file1 while the second is from file2. In both cases, <byte number> is relative to the beginning of the file, beginning with 1.

No output will be written to standard output when the -s option is used.


Used only for diagnostic messages. If file1 and file2 are identical for the entire length of the shorter file, in the POSIX locale the following diagnostic message will be written, unless the -s option is specified:

"cmp: EOF on %s%s\n", <name of shorter file>, <additional info>

The <additional info> field is either null or a string that starts with a blank character and contains no newline characters. Some systems report on the number of lines in this case.






The following exit values are returned:
The files are identical.
The files are different; this includes the case where one file is identical to the first part of the other.
An error occurred.




Although input files to cmp can be any type, the results might not be what would be expected on character special device files or on file types not described by the XSH specification. Since this specification does not specify the block size used when doing input, comparisons of character special files need not compare all of the data in those files.






comm, diff.

UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group
[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]