Linux “cmp” Command Examples

9

The “cmp” command in Linux is used to compare the contents of the two files hence reporting whether the two files are identical or different. This command comes with multiple options that can be paired with it to customize its output. Let us talk about this command in detail in this article to master its usage.

Syntax and Help Manual of the “cmp” command in Linux:

The syntax of the “cmp” command is as follows:

We can use different options with this command to get the desired output, whereas File1 and File2 represent the file names of the two files to be compared.

You can also read its help manual by executing this command:

The help manual of this command is shown in the image below:

Examples of the “cmp” command in Linux:

We have listed the five most common examples of using the “cmp” command in Linux over here. However, before proceeding with these examples, we would like to share the two text files that we will use in all of these examples. We have created these files in our home directory, and you can quickly analyze their contents to see their differences. These text files are shown below:

Example # 1: Simple Comparison of Two Files:

We can perform a simple comparison of the two files to check out if they differ from each other or not by using the following command:

We have replaced File1 with List.txt and File2 with List2.txt. The output of this command reveals that our two specified text files are different from each other.

Example # 2: Comparing Files after Skipping a Specified Number of Bytes:

You can also choose to compare two files after skipping a certain number of bytes. This can prove helpful in situations where you know for sure that the first few bytes of both the files are identical or are not concerned with them at all. Therefore, there is no need to compare those bytes. You can do this by using the command stated below:

$ cmp –i INT File1 File2

We have replaced File1 with List.txt and File2 with List2.txt. “INT” represents the number of bytes to be skipped, which we wanted to be “2” in our case. Again, the output of this command reveals that our two specified text files are different from each other.

Example # 3: Comparing the First “n” Number of Bytes of the Files:

At times, you only want to compare the first few bytes of two files. In such cases, you do not need to compare the entire contents of the two files. You can achieve this functionality by using the following command:

$ cmp –n INT File1 File2

We have replaced File1 with List.txt and File2 with List2.txt. “INT” represents the number of bytes to be compared, which we wanted to be “5” in our case. However, the output of this variation of the “cmp” command is interestingly different. Here, we are only comparing the first five bytes of both the files, and since the first five bytes of both our files were identical, that is why we will not receive any message in the output as shown in the image below:

Example # 4: Display the Differing Bytes of the Files in the Output:

You can also choose to display the differing bytes of the files in the output of the “cmp” command in the following manner:

We have replaced File1 with List.txt and File2 with List2.txt. The differing bytes of our specified files are shown in the output below:

Example # 5: Display Byte Numbers and Differing Byte Values of the Files in the Output:

For listing down all the differing byte numbers along with the differing byte values in both the files, you can use the “cmp” command in the following manner:

We have replaced File1 with List.txt and File2 with List2.txt. All the differing byte numbers, along with their values, are shown in the output below. Here, the first column represents the byte numbers of the differing bytes in both the files, whereas the first and second columns represent the byte values of the differing bytes in the first and second files, respectively.

Conclusion:

We can quickly compare two Linux files using the “cmp” command as described in this article. It is a handy command, especially while working with files, as it helps a lot in analyzing their contents.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More