cmp
PURPOSE
Compares two files.
SYNOPSIS
cmp [ -l ] [ -s ] file1 file2
DESCRIPTION
The cmp command compares file1 and file2 and writes the
results to standard output. If you specify a - (minus)
for file1, cmp reads standard input. Under default con-
ditions, cmp displays nothing if the files are the same.
If they differ, cmp displays the byte and line number at
which the first difference occurs. If one file is an
initial subsequence of the other (that is, if cmp reads
an end-of-file character in one file before finding any
differences), cmp notes this. Normally, you use cmp to
compare non-text files and the diff command to compare
text files.
FLAGS
-l Displays, for each difference, the byte number in
decimal and the differing bytes in octal.
-s Returns only an exit value. (0 indicates identical
files; 1 indicates different files; 2 indicates
inaccessible file or a missing argument)
EXAMPLES
1. To determine whether two files are identical:
cmp prog.o.bak prog.o
This compares "prog.o.bak" and "prog.o". If the
files are identical, then a message is not displayed.
If the files differ, then the location of the first
difference is displayed.
For instance:
prog.o.bak prog.o differ: char 5, line 1
If the message "cmp: EOF on prog.o.bak" is displayed,
then the first part of "prog.o" is identical to
"prog.o.bak", but there is additional data in
"prog.o".
2. To display each pair of bytes that differ:
cmp -l prog.o.bak prog.o
This compares the files, and then displays the byte
number (in decimal) and the differing bytes (in
octal) for each difference. For example, if the
fifth byte is octal "101" in "prog.o.bak" and "141"
in "prog.o", then "cmp" displays:
5 101 141
3. To compare two files without writing any messages:
cmp -s prog.c.bak prog.c
This gives an exit value of "0" if the files are
identical, "1" if different, or "2" if an error
occurs. This form of the command is normally used in
shell procedures. For example:
if cmp -s prog.c.bak prog.c
then
echo No change
fi
This partial shell procedure displays "No change" if
the two files are identical. See page for details
about the if command.
RELATED INFORMATION
The following commands: "comm," "diff," and "sh."