CMP Command in Linux/UNIX
The cmp
command in Linux/UNIX is used to compare two files byte by byte, helping determine whether the two files are identical or not. When used for comparison, cmp
reports the location of the first mismatch if differences are found. If the files are identical, cmp
display no message and return the prompt.
Syntax:
cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]
OPTION
: Optional flags that control the behavior of the command.FILE1
andFILE2
: Filenames of the two files to be compared.SKIP1
andSKIP2
: Optional number of bytes to skip at the beginning of each file (default is 0).
Example:
$ cmp file1.txt file2.txt
cmp
Command:-b
(print-bytes): Displays differing bytes in the output when used with -b
option.
$ cmp -b file1.txt file2.txt file1.txt file2.txt differ
-i
[bytes-to-be-skipped]: Skips a specified number of initial bytes from both files and then compares them.
$ cmp -i 10 file1.txt file2.txt
-i
[bytes-to-be-skipped1:bytes-to-be-skipped2]: Skips several bytes separately from each file.
$ cmp -i 10:12 file1.txt file2.txt
-l
(list): Prints byte position and byte value for all differing bytes.
$ cmp -l file1.txt file2.txt
-s
(silent): Compares files without writing any messages. Exit values: 0 (identical), 1 (different), 2 (error).
$ cmp -s file1.txt file.txt
-n
[number of bytes]: Limits the number of bytes to be compared.
$ cmp -n 50 file1.txt file2.txt
--version
: Outputs version information and exits.
$ cmp --version
--help
: Displays a help message and exits.
$ cmp --help
These options provide flexibility in how the cmp
the command operates, allowing users to tailor the comparison based on their specific needs.