The “cmp” Command in Linux [7 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The cmp command in Linux is used for comparing two files byte by byte and displays the difference between the files. The command helps you to know whether the files’ contents are the same or not. In this section, I will demonstrate the syntax, options, and practical applications of the cmp command in Linux.

A. Description

Using the cmp command in Linux, you’ll find whether the two files are identical or not. This command basically compares the file’s byte by byte. There’s no output in the terminal screen if the files are identical but if the files are different the output will display the difference between the two files in the terminal.

B. Syntax

The syntax of the cmp command in Linux is very easy. And the syntax is as follows:

cmp [OPTION]... [FILE1][FILE2]
Note: In the syntax above, OPTION is enclosed by a square bracket and followed by 3 dots representing that more than one option can be used simultaneously. Moreover, FILE1 & FILE2 suggest that the only two files to be compared can be specified here.

C. Options

There are some options available for the cmp command in Linux. Some of them are listed below. Although you can explore the man page for the cmp command to learn more about its options.

man cmp

Useful Options:

  • -b, Prints differing bytes of files
  • -i, Compares after skipping the specified number of byte
  • -l, Prints differing bytes of files while Comparison
  • -n, Limits the number of bytes to be compared
  • -v, Displays version info
Note: The options in Linux CLI (Command Line Interface) are case-sensitive, so be cautious when you use them.

Practical Examples of the “cmp” Command in Linux

The cmp command in Linux is easy as pie. This command compares two files byte by byte to see whether the files’ contents are the same or not. Here you will get to know some useful examples of the cmp command. In this article, I have worked with the following files, you can also download those files to work with the same files.

Example 1: Compare Two Identical Files Using the “cmp” Command in Linux

To compare two identical files named 1.txt & 2.txt I am using the cmp command in Linux. To do the same you can follow the procedure below:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cmp 1.txt 2.txt

➌ Now, press the ENTER button.

❹ To check the files manually, type the following command in the command prompt:

cat 1.txt

&

cat 2.txt

➎ Finally, press the ENTER button.

Output >

In the image below, you can see that there’s no output as the two files are identical.Compare Two Identical Files Using the “cmp” Command in Linux The following image shows the contents of both files are the same.Compare Two Identical Files manually


Similar Readings


Example 2: Compare Two Different Files Using the “cmp” Command in Linux

To compare two different files named 1.txt & 2.txt you can use the cmp command in Linux. To do the same you can follow the procedure below:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cmp 1.txt 2.txt

➌ Now, press the ENTER button.

❹ To check the files manually, type the following command in the command prompt:

cat 1.txt

&

cat 2.txt

➎ Finally, press the ENTER button.

Output >

In the image below, you can see the output displays the two files are different.Compare Two Different Files Using the “cmp” Command in LinuxThe below image shows the contents of the files are different.Compare Two Different Files manually

Example 3: Print Differing Bytes of Files While Comparing

To print differing bytes while comparing two files named 1.txt & 2.txt you can use the cmp command along with option -b in Linux.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cmp -b 1.txt 2.txt

➌ Now, press the ENTER button.

Output >

In the following image, the output displays the differing bytes of the two files.Print Differing Bytes of Files While Comparing

Example 4: Compare Two Files Skipping Specified Number of Bytes

To compare two files named 1.txt & 2.txt after skipping a specified number of bytes I am using the cmp command with option -i in Linux. To do the same you can follow the procedure below:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cmp -i 5 1.txt 2.txt

➌ Now, press the ENTER button.

Output >

In the following image, the output displays the two files are compared after skipping 5 bytes.Compare Two Files Skipping Specified Number of Bytes

Example 5: Print Byte Number & Value of Differing Bytes of The Compared Files

To print the byte number & value of the differing bytes of the compared files named 1.txt & 2.txt you can use the cmp command with option -l in Linux. To do the same you can follow the procedure below:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cmp -l 1.txt 2.txt

➌ Now, press the ENTER button.

Output >

In the following image, the output displays the byte number & values of the differing bytes of the compared files.Print Byte Number & Value of Differing Bytes of The Compared Files


Similar Readings


Example 6: Limit the Number of Bytes to be Compared

To limit the number of bytes of the compared files named 1.txt & 2.txt I am using the cmp command with option -n in Linux. By limiting to n number of bytes it will just compare the first n bytes of the files. To do the same you can follow the procedure below:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cmp -n 3 1.txt 2.txt

➌ Now, press the ENTER button.

Output >

As you can see in the following image, the first 3 bytes of the files are compared. As the first 3 bytes of the files are identical that’s why there’s no output.Limit the Number of Bytes to be Compared

Example 7: View Version Info Using the “cmp” Command in Linux

To view the version info I am using the cmp command with option -v . To do the same you can follow the procedure below:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

cmp -v

➌ Now, press the ENTER button.

Output >

As you can see in the image below, the output displays the version info.View Version Info Using the “cmp” Command in Linux

Conclusion

In this section, I have tried to show you the cmp command with some practical examples in Linux. You’ve also discovered the syntax and some functional options of this command. To become proficient in Linux, explore the command and its practical applications thoroughly.


Similar Readings

 

3/5 - (1 vote)
Jannatul Ferdousi Sylvie

Hi there! This is Jannatul Ferdousi Sylvie. I am one of the Linux Content Developer Executives at Softeko. I live in Dhaka, Bangladesh. I have completed my BSc. in Software Engineering from American International University-Bangladesh. You can see my projects in the GitHub account. I love traveling, shopping, cooking, and gardening. Read Full Bio

Leave a Comment