8.4.4 cmp command (compares binary files)
Syntax
cmp [-l|-s] path-name-1 path-name-2 [skip-1 [skip-2]]
Description
This command compares binary files. Differences in byte values and the locations where the differences occur can be displayed.
Arguments
If the -l and -s options are both omitted, the command displays only the location where the first difference is detected. An error results if the -l and -s options are both specified.
- -l
Specifies that each set of a difference in byte values (octal) and their offset in bytes (decimal) are to be displayed.
- -s
Specifies that only a termination status indicating whether the files are different is to be returned.
- path-name-1
Specifies the path name of the comparison source file. If you specify - for path-name-1, the contents for comparison are read from the standard input.
- path-name-2
Specifies the path name of the comparison target file. If you specify - for path-name-2, the contents for comparison are read from the standard input.
- skip-1
Specifies the position (in bytes) in the file in path-name-1 that is to be the beginning point of the comparison processing.
- skip-2
Specifies the position (in bytes) in the file in path-name-2 that is to be the beginning point of the comparison processing.
Return codes
Return code |
Meaning |
---|---|
0 |
Normal termination. The files are identical. |
1 |
Normal termination. The files are different. Or, the end-of-file (EOF) was reached earlier in one file than in the other, in which case a message (cmp: EOF on file-name) is output. |
2 or greater |
Error termination |
Notes
The end-of-line code [CR] + [LF] is considered two bytes.
In Windows, input from a file or from the standard input is performed in binary mode. No conversion of end-of-line codes is performed.
Examples
These usage examples illustrate the results of executing the cmp command on the files abc.txt and abcd.txt, whose contents are shown below. In the examples, represents a space and represents a tab character.
abc.txt
aaaaaaaaaaa bbbbbbbb cccccccccccccccc dddddddddddd
abcd.txt
aaaaaaaaaaa bbbbbbbb cccccccccccccccc dddddddddddd eeeeeeeeeeeeeeeee
The files listed above are the input files in the following examples, which illustrate the results of executing the cmp command.
Specify the -l option to display the offset in bytes (decimal) and the values (octal) of each difference between abc.txt and abcd.txt.
C:\TEMP>%ADSH_OSCMD_DIR%\cmp -l abc.txt abcd.txt 49 12 40 50 11 40 51 11 40 52 11 40 53 12 40 54 12 40 65 40 12 66 12 40 67 144 40 68 144 40 69 144 40 70 144 40 71 144 40 72 144 40 73 144 40 74 144 40 75 144 40 76 144 40 77 144 40 78 144 40 79 12 40 cmp: EOF on abc.txt
Specify the -s option to return the return code without displaying the results.
C:\TEMP>%ADSH_OSCMD_DIR%\cmp -s abc.txt abcd.txt
Specify the optional skip arguments to set the starting bytes for comparison to 3 for each file. In the first example, the skip arguments are omitted. In the second example, they are set to 3.
C:\TEMP>%ADSH_OSCMD_DIR%\cmp abc.txt abcd.txt abc.txt abcd.txt differ: char 49, line 7 C:\TEMP>%ADSH_OSCMD_DIR%\cmp abc.txt abcd.txt 3 3 abc.txt abcd.txt differ: char 46, line 7
Display an option error message.
This message might vary depending on the platform on which the command is executed. The following shows an example in Windows:
C:\TEMP>%ADSH_OSCMD_DIR%\cmp -w cmp: illegal option -- w usage: cmp [-l | -s] file1 file2 [skip1 [skip2]]
Display an error message if the file does not exist:
C:\TEMP>%ADSH_OSCMD_DIR%\cmp file99 file123 cmp: file99: No such file or directory