Linux CMP Command Explained

The cmp command in Linux/UNIX compares two files byte by byte, allowing you to determine if they are identical or not. If a difference is detected, cmp displays the location of the first mismatch on the screen,if no difference is found, cmp displays the files are identical.

cmp shows no message and basically returns the brief assuming the files analyzed are identical. A lot of options are available for the cmp command to be paired with to display different output , these options are explained below. Also, I am going to show you the practical uses of cmp command.

The Syntax of cmp command

Syntax:cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]

SKIP1 ,SKIP2 & OPTION are optional and FILE1 & FILE2 refer to the filenames .

The syntax of the cmp command is straightforward. We'll clearly need their names as arguments if we're comparing two files (i.e as FILE1 & FILE2 in syntax). SKIP1 and SKIP2 provide the number of bytes to skip at the beginning of each file, which is 0 by default, and OPTION refers to the options compatible with this command, which will be shown in the article later.

Example of cmp command

If a difference is observed, the cmp command reports the byte and line number, as previously stated. Let's investigate this further using an example. Here, Linux.txt and Linux1.txt are the two files that are being compared.

cmp Linux.txt Linux1.txt

(Note: Linux.txt and Linux1.txt are the file that are being compared throughout the article)

The above command will give the result as shown in the figure if the files are identical to each other.

If the file is not identical then the result will be as shown in figure.

You can see the options of cmp command, using the help menu as below:

cmp - -help

Explanation of cmp command in linux terminal

cmp command to print bytes(-b (--print-bytes))

To display the differing bytes in the output, use the -b options.Here ,the value 40 and 56 are the bytes of this file.

cmp -b Linux.txt Linux1.txt

cmp command to skip initial bytes -i(--ignore-initial=SKIP)

The user will be allowed to skip a particular number of bytes from the file ,when using -i command with cmp.(Note,enter the number of bytes you want to skip in the command to execute it).

cmp -i 79 Linux.txt Linux1.txt

cmp command to be skipped from first file -i(--ignore-initial=SKIP1:SKIP2)

This command has a similar use as the above command, but the difference is it allows the user to input the number of bytes they want to skip for both files. Here, after skipping 79 and 54 from the first and second file respectively,the files are identical. (Note,enter the number of bytes you want to skip in the command to execute it).

cmp -i 79:54 Linux.txt Linux1.txt

cmp command to print position and value -l(--verbose)

The bytes position and value are printed using the -l option in cmp command.The output indicate that the files are different , displaying the position of different bytes and the differing bytes in the given files.The first column indicate the position i.e. byte number of differing bytes. The second one indicates the byte value of differing bytes in the first file and the third column indicates the byte value of the second file.

cmp -l Linux.txt Linux1.txt

cmp command to compare bytes -n (--bytes=LIMIT)

The comparison of the bytes is limited using the -l option in cmp command. Here, the comparison is done in only 50 bytes.

cmp -n 50 Linux.txt Linux1.txt

cmp command to compare file without any messages -s (--quite,--silent)

-s option in cmp command allows the output to be silent ,that means, without writing any messages it compares the files. If the files are identical it executes the value 0 , if it is different it executes the value1 and if any error is there it executes the value 2.

cmp -s Linux.txt Linux1.txt

Leave a Comment