10+ cmp command examples in Linux [Cheat Sheet]

Introduction to cmp command

cmp is a command-line utility in a Linux system to compare two files byte by byte. It prints the byte and line number where the first difference is found. It prints nothing if no differences are found.

 

Different examples to use cmp command

As cmp compares two files, it takes two file names as arguments. The syntax of cmp command is:

Advertisement
cmp [option] file1 [file2 [SKIP1 [SKIP2]]]

SKIP1 and SKIP2 are the number of bytes to skip in each file.

In this article, we will use cmp command to compare two files byte by byte using different available options.

 

1. cmp command to compare two files

You can use cmp command without any options to compare two files byte by byte. When differences are found between two files, it prints the bytes and line number where the first difference is found.

$ cmp file1 file2

Sample Output:

Here, the first difference is found at 145 bytes in line number 3.

ubuntu@golinux:~/testdir$ cmp doc1 doc2
doc1 doc2 differ: byte 145, line 3

If no difference is found, it prints nothing in the output.

Advertisement
ubuntu@golinux:~/testdir$ cmp doc2 doc3

 

2. cmp command to compare two symbolic links

We can also use cmp command to compare two symbolic links, or symbolic link with a file using the same syntax as used above. For example here we have two symbolic links:

10+ cmp command examples in Linux [Cheat Sheet]

Let's compare these two symbolic links and symbolic link with the parent file:

10+ cmp command examples in Linux [Cheat Sheet]
As you see, we get a blank output which means bot files are same.

 

3. cmp command to print differing bytes

-b or --print-bytes option displays the differing bytes in the output.

$ cmp -b file1 file2

OR

$ cmp --print-bytes file1 file2

Sample Output:

The difference is found at byte 145 in line number 3. doc1 contains 'r' whereas doc2 contains 'i'.  162 and 151 are the byte values.

ubuntu@golinux:~/testdir$ cmp -b doc1 doc2
doc1 doc2 differ: byte 145, line 3 is 162 r 151 i

 

4. cmp command to display all differing bytes

You can use -l or --verbose option to print byte numbers and values for all differing bytes.

Advertisement
$ cmp -l file1 file2

OR

$ cmp --verbose file1 file2

Sample Output:

The first column contains the byte numbers. The second and third columns show bytes values of doc1 and doc2, respectively.

ubuntu@golinux:~/testdir$ cmp -l doc1 doc2
145 162 151
146 145 163
147 160 40
148 157 141
149 162 40
150 164 146
151 163 162
...
256 157 171
257 165 163
258 156 164
259 144 145
260 56 155
261 12 56
cmp: EOF on doc1 after byte 261

 

5. Skip the first bytes of files with cmp command

-i or --ignore-initial option allows you to skip the first N number of bytes in both files.

$ cmp -i N file1 file2

OR

$ cmp --ignore-initial=N file1 file2

Sample Output:

After skipping 150 bytes, the first difference is found at byte 1 in line number 1.

ubuntu@golinux:~/testdir$ cmp -i 150 doc1 doc2
doc1 doc2 differ: byte 1, line 1

6. cmp command to skip bytes of two files separately

With the above command, you can specify the same number of bytes to be ignored. You can use the following command to select the different bytes to be skipped in each file.

$ cmp -i N1:N2 file1 file2 

OR

$ cmp --ignore-initial=N1:N2 file1 file2

Sample Output:

ubuntu@golinux:~/testdir$ cmp -i 150:180 doc1 doc2
doc1 doc2 differ: byte 1, line 1

To skip bytes without using -i option, you can use:

$ cmp file1 file2 N

OR

$ cmp file1 file2 N1 N2
ubuntu@golinux:~/testdir$ cmp doc1 doc2 150
doc1 doc2 differ: byte 1, line 1

ubuntu@golinux:~/testdir$ cmp doc1 doc2 150 180
doc1 doc2 differ: byte 1, line 1

 

7. cmp command to compare limited bytes

-n or --bytes option allows us to limit the number of bytes to compare between two files.

$ cmp -n LIMIT file1 file2

OR

$ cmp --bytes=LIMIT file1 file2

Sample Output:

-l option is used to show all differing bytes. As we can see, the command compares two files to 160 bytes only.

ubuntu@golinux:~/testdir$ cmp -l -n 160 doc1 doc2
145 162 151
146 145 163
147 160 40
148 157 141
149 162 40
150 164 146
151 163 162
152 40 151
153 164 145
154 150 156
155 145 144
156 40 154
157 142 151
158 171 145
159 164 162
160 145 40

 

8. cmp command to suppress the output

If you want to hide the output displayed by cmp command, you can use -s, --quiet, or --silent option.

$ cmp -s file1 file2

OR

$ cmp --quiet file1 file2

OR

$ cmp --silent file1 file2

Sample Output:

ubuntu@golinux:~/testdir$ cmp -s doc1 doc2
ubuntu@golinux:~/testdir$ cmp --quiet doc1 doc2
ubuntu@golinux:~/testdir$ cmp --silent doc1 doc2

 

9. Suppress error messages from cmp command

Using -s argument we can suppress messages which gets printed on STDOUT i.e. successful use cases. But any failure or error message will not be suppressed using -s switch.

For example, here we intentionally try to compare more than two files when we know only 2 files can be compared so we get an error on the console:

10+ cmp command examples in Linux [Cheat Sheet]

As you can see, even using -s switch the error was not suppressed. So in such case we have to send the output to /dev/null to suppress the output:

10+ cmp command examples in Linux [Cheat Sheet]

So now we don't get any error output.

 

10. cmp command to print the version information

You can get the version information of cmp using -v or --version option.

$ cmp -v 

OR

$ cmp --version

Sample Output:

ubuntu@golinux:~/testdir$ cmp -v
cmp (GNU diffutils) 3.7
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Torbjörn Granlund and David MacKenzie.

 

11. Use cmp command in shell script to compare files

cmp command is most useful inside scripts where we want to compare two files for their content. Here is a sample shell script which chceks if two files have same content or not:

#!/bin/bash

FILE1=/tmp/file1
FILE2=/tmp/file2

cmp -s $FILE1 $FILE2
if [ $? -eq 0 ];then
   echo "Both files have same content"
elif [ $? -eq 1 ];then
   echo "Both files have different content"
else
   echo "Some error occurred"
fi

Let us execute this script:

$ sh /tmp/compare_file.sh
Both files have different content

Since in my case, both files had different content so cmp command has accordingly given the status. Following are the valid EXIT STATUS:

  • 0 The files are identical.
  • 1 The files are different; this includes the case where one file is identical to the first part of the other.
  • >1 An error occurred.

 

Conclusion

This article teaches you how to use cmp command to compare two files byte by byte. We hope now you will be able to use cmp command in the Linux system. If you still have any confusion, you can ask us in the comment section.

 

What's Next

cat command examples for beginners [cheatsheet]

 

Further Reading

man page for cmp command

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment