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:
All differences are reported in the following form:
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. 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.
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.
The names of the files that you wish to compare.
|
Exit status
0 | files identical |
1 | files differ |
>1 | inaccessible file or missing argument |
Error
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).
|
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
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 |