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]
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
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. The following image shows the contents of both files are the same.
Similar Readings
- The “diff” Command in Linux [11 Practical Examples]
- The “comm” Command in Linux [10 Practical Examples]
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.The below image shows the contents of the files are different.
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.
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.
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.
Similar Readings
- The “ln” Command in Linux [6 Practical Examples]
- The “lsof” Command in Linux [8 Practical Examples]
- The “tar” Command in Linux [12 Practical Examples]
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.
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.
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
- The “chgrp” Command in Linux [7 Practical Examples]
- The “file” Command in Linux [9+ Practical Examples]
- The “fsck” Command in Linux [7 Practical Examples]
- The “patch” Command in Linux [4 Practical Examples]
- The “rmdir” Command in Linux [7 Practical Examples]
- The “stat” Command in Linux [9 Practical Examples]
FUNDAMENTALS A Complete Guide for Beginners