Cmp Command in Linux

FREE Online Courses: Elevate Your Skills, Zero Cost Attached - Enroll Now!

In this article, we will learn everything about the cmp command in Linux-based operating systems. We will look at what the cmp command is, why it is used, how to install it, the syntax of the cmp command, and the options used along with it.

In the end, we will also look at some fantastic practical examples of the cmp command by pairing it with various options to understand its working. So pay attention, take notes, and read all the way to the end for the best benefits.

What is cmp command in Linux?

The cmp command is short for “compare”, and as the name suggests, cmp is a command line-based utility in Linux-based operating systems that helps in comparing 2 files byte by byte.

The cmp command is an excellent command to find the differences between 2 files. Upon comparing the 2 files, the cmp command prints the byte and line number where the first difference is found, and if it finds no differences between them, it simply prints nothing.

Now there are far more superior tools for comparing files, one fine example is the “diff” command, which not only compares the 2 files but also tells you what to change in the files to make both the files identical. Nonetheless, the cmp command gets the job done and in this article, we will explore everything about the cmp command in Linux.

Syntax of Linux cmp command

The syntax of the cmp command might look slightly intimidating at first, but once we understand the fields present in the syntax, it will become a cakewalk! The syntax of the cmp command is shown below:

cmp <OPTIONS> <FILE1 <FILE2 <SKIP1 <SKIP2>>>

The fields <options>, <skip1> and <skip2> are optional, but every cmp command you run must have 2 files to compare. Let us take a closer look at the fields present in the syntax of the cmp command.

1. <OPTIONS>

This field takes in a range of options that specify how the cmp command must function, format, and print the output.

2. <FILE1> and <FILE2>

These fields specify the name of the file to be compared against each other. You can also specify the paths of these files if they are not present in your current working directory.

3. <SKIP1> and <SKIP2>

These fields specify the number of bytes to be skipped at the beginning of each file. If you don’t specify any number, the default will be taken as zero.

Options used with Linux cmp command

Unlike most commands in Linux, the cmp command comes with very few options that specify how the cmp command must function, format, and print the output. Let us take a detailed look at each of these options.

1. -b

This option prints the differing bytes. You can also write this option as “–print-bytes”

2. -i

This option skips the specified number of bytes from the beginning of the file. You can also write this option as “–ignore-initial”

3. -h

This option displays the help menu of the cmp command as shown below:

 

help menu of the cmp command

4. -n

This option compares at most the specified number of bytes, it will not exceed the limit you specify. In simple terms, this option compares both the files from the beginning to the position of the byte you specified. You can also write this option as “–bytes”

5. -l

This option provides verbose to the output to get a better picture of what is happening. You can also write this option as “–verbose”

6. -s

This option enables quiet mode, where very less output is printed on the screen. This option also suppresses errors. You can also write this option as “–quiet”

7. -v

This option displays information regarding the version of the cmp command you are using on your PC as shown:

version of the cmp command

Now, since we have covered the theory and fundamentals regarding the cmp command, let us look at some fantastic examples of the cmp command in the terminal of ubuntu 20.04.

Actually, before we proceed with the examples, we will be needing 2 sample text files to compare their difference. Hence let us compare the following 2 files with one difference: the word “NO” is missing in the 5 line of the second file:

sample text files to test the cmp command

 

Comparing 2 files in Linux

If you want to compare 2 files, you can simply use the following syntax:

“cmp <file1> <file2>”

For example, the command “cmp file1.txt file2.txt” compares the differences and prints the line number and byte number of the difference as shown in the picture below:

comparing 2 files

Using file path to compare

If you are in another directory and want to compare files that are not present in your working directory, you can specify the paths of the files as shown in the picture below:

using file path to compare

Printing the differing bytes

If you want the cmp command to print the differing bytes, run it with the option “-b” as shown:

cmp -b <file1> <file2>

In the examples below, the difference found is that in file1.txt there is the letter “N”, whereas in file2.txt, there is the letter “W”.

printing the differing bytes

Displaying all the differing bytes

Pairing the cmp command with the option “-l” prints the byte numbers and values for all differing bytes. In the output, the first column contains the byte numbers, the second and third column contain the bytes values of doc1 and doc2

displaying all the differing bytes

Skipping the first bytes of both files

If you want to ignore the first “n” bytes of both the files, you can run the cmp command by pairing it with the option “-i” followed by the number of bytes you want to ignore as shown:

cmp -i n <file1> <file2>

skipping the first bytes of both files

Skipping bytes of files separately

If you want to ignore the first “n1” bytes of file1 and the first “n2” bytes of file 2, you can run the cmp command by pairing it with the option “-i” followed by the number of bytes you want to ignore as shown below:

cmp -i <n1>:<n2> <file1> <file2>

skipping bytes of files separately

Another method you can use to to the same is to use the <skip> we learned earlier by using the following syntax:

cmp <file1> <file2> <n1> <n2>

another method to skip bytes of files separately

Comparing limited bytes

If you want to compare only a range of the bytes in both the files, pair the cmp command with the option “-n” as shown in the syntax below:

cmp -n <LIMIT> <file1> <file2>

Suppressing the output

If you want to suppress the output of the cmp command use the option “q”.

suppressing the output

Using Linux cmp command in shell programs

We can also use the cmp command in shell scripting! Here is one fine program making good use of the cmp command. In this program, if file one and file 2 are the same, it will print “Both files have the same content” and if there are any differences, it will print “Both files have different content”

#image#

Summary

As you have seen, the cmp command is a simple tool that lets you compare two files and prints the byte and line number of the difference. You have now understood what the cmp command is, why it is used, the syntax of the cmp command, and the various options used with it along with examples.

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *