The “cmp” command in Linux is applied to compare the contents of the two files that’s why reporting whether or not the two information are identical or distinct. This command arrives with multiple options that can be paired with it to personalize its output. Enable us communicate about this command in depth in this short article to master its use.
Syntax and Aid Guide of the “cmp” command in Linux:
The syntax of the “cmp” command is as follows:
We can use different selections with this command to get the sought after output, while File1 and File2 symbolize the file names of the two files to be in comparison.
You can also study its help guide by executing this command:
The assistance handbook of this command is revealed in the graphic below:
Examples of the “cmp” command in Linux:
We have outlined the 5 most prevalent examples of making use of the “cmp” command in Linux over listed here. On the other hand, prior to proceeding with these examples, we would like to share the two textual content information that we will use in all of these examples. We have made these information in our home directory, and you can rapidly assess their contents to see their variations. These text files are demonstrated below:
Illustration # 1: Easy Comparison of Two Documents:
We can perform a very simple comparison of the two documents to check out if they vary from each other or not by using the following command:
We have replaced File1 with Record.txt and File2 with Record2.txt. The output of this command reveals that our two specified text documents are various from just about every other.
Case in point # 2: Comparing Data files immediately after Skipping a Specified Quantity of Bytes:
You can also opt for to evaluate two files immediately after skipping a sure selection of bytes. This can establish handy in conditions exactly where you know for positive that the initially couple bytes of the two the information are equivalent or are not involved with them at all. As a result, there is no will need to compare these bytes. You can do this by applying the command mentioned beneath:
$ cmp –i INT File1 File2
We have replaced File1 with List.txt and File2 with Record2.txt. “INT” represents the amount of bytes to be skipped, which we wished to be “2” in our circumstance. Once more, the output of this command reveals that our two specified textual content files are different from each individual other.
Case in point # 3: Evaluating the To start with “n” Amount of Bytes of the Documents:
At periods, you only want to review the first handful of bytes of two documents. In these kinds of conditions, you do not require to evaluate the total contents of the two files. You can achieve this functionality by making use of the following command:
$ cmp –n INT File1 File2
We have replaced File1 with Listing.txt and File2 with Record2.txt. “INT” represents the amount of bytes to be when compared, which we wanted to be “5” in our circumstance. However, the output of this variation of the “cmp” command is apparently unique. Right here, we are only evaluating the initially five bytes of the two the documents, and given that the very first 5 bytes of both our information were identical, that is why we will not get any information in the output as proven in the graphic beneath:
Instance # 4: Display the Differing Bytes of the Documents in the Output:
You can also opt for to exhibit the differing bytes of the data files in the output of the “cmp” command in the pursuing manner:
We have replaced File1 with Record.txt and File2 with List2.txt. The differing bytes of our specified files are proven in the output below:
Case in point # 5: Show Byte Numbers and Differing Byte Values of the Documents in the Output:
For listing down all the differing byte figures along with the differing byte values in both equally the data files, you can use the “cmp” command in the next manner:
We have changed File1 with Record.txt and File2 with Checklist2.txt. All the differing byte quantities, together with their values, are demonstrated in the output underneath. In this article, the initial column signifies the byte quantities of the differing bytes in both of those the documents, while the first and second columns represent the byte values of the differing bytes in the initially and next data files, respectively.
We can promptly evaluate two Linux documents working with the “cmp” command as explained in this report. It is a useful command, particularly though performing with files, as it allows a large amount in examining their contents.