The “cmp” command is a simple yet powerful feature used to compare the contents of two files and determine if they have similar content. The “cmp” command is particularly useful for system administrators and developers who need to verify the integrity of files or compare the contents of different file versions.
This article will discuss the detailed usage of the “cmp” command with the following outcomes:
- How to Use the cmp Command in Linux?
- Compare Two Files Line-by-Line
- Compare Two Files Byte-by-Byte
- Show All the Different bytes Between the Two Files
- Use bash Script to Compare Two Strings
How to Use the cmp Command in Linux?
The “help” flag in most commands presents the basic syntax and the options the command supports. Before getting into details, let’s have a look at the cmp command’s help page:
$ cmp --help
So, when you run either of the above commands, it will give you the syntax along with different options associated with it that will be discussed later in this article. So from the above image, the basic syntax of the cmp command is as follows:
$ cmp [options]...[file1 file2…..]
Now, let’s discuss some real examples of the cmp command in the next to give you a better understanding.
Example 1: Compare Two Files Line-By-Line
In this case, we have created two different text files, but the data inside them is the same. So when you compare the data of these two files with the cmp command, it will show you no output as shown below:
$ cmp file1.txt file2.txt
It is worth noting that the basic cmp command is used to compare two files line by line. Now let’s see what it will show you as an output when the text of these two files is different:
$ cmp file1.txt file2.txt
Now it is showing you that the text of these two files is different, whereas the “byte1” in the output refers to the byte number at which the first difference between the two files was detected. The “line1” in the output refers to the line number at which the first difference between the two files was detected.
Example 2: Compare Two Files byte-by-byte
When you use the ‘-b‘ option along with the cmp command, then it will compare the text byte-by-byte rather than line-by-line that can be done by using the syntax mentioned below:
$ cmp -b file1.txt file2.txt
The output is described as:
- The “byte 12” refers to the byte number at which the first difference between the two files was detected.
- The “line 1” in the output refers to the line number at which the first difference between the two files was detected.
- The “is 151 i 154 l” shows the character representation of the first differing byte in both files. In this case, the first differing byte in file1.txt is the character “151,” and in file2.txt is the character “154,” and it’s a letter “l”.
Example 3: Show All the Different Bytes
The “-l” option is used to display the byte and line number at which the first difference occurs between the two files by typing the command mentioned below:
$ cmp -l file1.txt file2.txt
The output shows the hexadecimal representation of the differing bytes between file1.txt and file2.txt, the byte offset and the line number (approximation) at which the first difference between the two files was detected, it also shows the message “cmp: EOF on file2.txt after byte 21” which means that the file2.txt has fewer bytes than file1.txt.
Example 4: Use bash Script to Compare Two Strings
You can also use the cmp command to compare strings in the bash script; for that, we have created a basic bash script.
# Prompt the user to enter the first string
read -p "Enter the first string: " string1
# Prompt the user to enter the second string
read -p "Enter the second string: " string2
# Compare the contents of the two strings
cmp <(echo "$string1") <(echo "$string2")
# Check the exit status of the cmp command
if [ $? -eq 0 ]; then
echo "The strings are identical."
echo "The strings are different."
In this script, we will enter two texts, and if the text is the same, then it will show you an output as “The strings are identical” else “The strings are different”. The “/dev/fd/63” and “/dev/fd/62” are file descriptor paths; the shell uses them to pass the two strings to the cmp command.
The “cmp” command is a powerful tool that enables users to compare the contents of two files and check if they are identical. It is especially useful for system administrators and developers as it can help them to verify the integrity of files or compare versions of a file. This post has explained the detailed usage of the “cmp” command in Linux.