|Previous||Table of Contents||Next|
The external cmp command compares two files. If the files are the same, cmp returns with no output. If the files are different, cmp returns the line number and byte position of the first difference. Only two files may be compared at one time. The unique features of cmp over other file comparison tools are:
Following is the general format of the cmp command.
cmp [ -ls ] - file2 cmp [ -ls ] file1 file2
The first format is used to compare the standard input to a file (file2). The second compares two existing files. You may find it helpful to use the first format in a pipe. The cmp file1 - format is not valid.
The following list describes the options that may be used to control how cmp functions.
|-l||Display the byte position in decimal and the bytes that differ in octal for each occurrence of a difference.|
|-s||Do not display information for differing files. Only set the return code to 1.|
The following arguments may be passed to cmp.
|-||Read from the standard input for the first input files.|
|file1||The first of two files to compare. This may be a text or binary type file.|
|file2||The second of two files to compare. This file may also be text or binary.|
If the - option is specified, you must enter input from your keyboard or pipe information to cmp. The -s option is useful if you only want to check the equality of two files. By checking the return code, using the $? ($status-csh) variable in the shell, you know whether the files are identical or different. The -l allows cmp to continue checking for differences throughout the file. By default, cmp only checks the files until the first difference is incurred, then it exits.
Refer to the comm and diff commands described in modules 22 and 33.
The return codes of cmp may be used to control conditional statements within shell scripts. The following list describes the meaning of each code.
|0||The files are identical.|
|1||The files are NOT identical.|
|2||There is a problem reading a file or with an option you provided.|
The cmp command is used to compare files for possible differences. You may want to think of it as the file comparison utility that can compare two files of any type for a difference of equality. It performs faster than the other file compare utilities. This is because it is a simple program comparing byte by byte. It can be thought of as a binary yes or no type file comparison program. Yes, the files are the same or no, the files are different. Thus it lends itself to shell scripts that need to check for files that match exactly, but they don't care what is wrong.
One example is the removal of like files. You can use the cmp command to compare files. If no output is returned (the return code is 0), you know the files are identical and thus can proceed to remove one of the files. On the other hand, if you want to make a backup copy of a file, you may want to use cmp to ensure the new copy is identical to the original. UNIX will usually complain if it did not make an identical copy. But if it adds security and bliss to your life, double-check the files!
Another example is trying to write a program that generates a predefined form. You may want to create the form in an ASCII file using an editor. Then generate the output of your program into a file and compare the new file with the predefined form using cmp. Once you have a matching blank form, you can continue writing your program to fill in the blank areas with variable output.
In this activity you use the cmp command to compare two files and check the status code. Begin at the shell prompt.
cj> cmp /bin/ls /bin/cat /bin/ls /bin/cat differ: char 2, line 1
|Previous||Table of Contents||Next|