Introduction
In this lab, you will learn how to use the cmp command in Linux to compare the contents of two files, both text and binary. The cmp command compares two files byte-by-byte and reports the first location where the files differ. You will create sample text and binary files, then use the cmp command to compare them and explore its various options to customize the comparison.
The lab covers the following steps:
- Understand the
cmpcommand and its basic usage. - Compare the contents of two text files using the
cmpcommand. - Compare the contents of two binary files using the
cmpcommand.
Understand the cmp Command
In this step, you will learn about the cmp command in Linux, which is used to compare the contents of two files. The cmp command compares two files byte-by-byte and reports the first location where the files differ.
To begin, let's create two text files with some content:
echo "This is file1.txt" > file1.txt
echo "This is file2.txt" > file2.txt
Now, let's use the cmp command to compare the two files:
cmp file1.txt file2.txt
Example output:
file1.txt file2.txt differ: byte 10, line 1
The output shows that the two files differ at byte 10, which corresponds to the 10th character in the file. This is because the two files have different content starting from the 10th character.
You can also use the cmp command to compare binary files. Let's create two binary files and compare them:
dd if=/dev/urandom of=file1.bin bs=1024 count=1
dd if=/dev/urandom of=file2.bin bs=1024 count=1
cmp file1.bin file2.bin
Example output:
file1.bin file2.bin differ: byte 1, line 1
In this case, the cmp command compares the two binary files byte-by-byte and reports the first location where they differ.
The cmp command also provides several options to customize the comparison, such as:
-l: Displays the byte number (decimal) and the differing bytes (octal) for each difference.-s: Silent mode, which does not output anything if the files are identical.-i: Ignores differences in case when comparing text files.
You can explore these options further to suit your specific needs.
Compare Two Text Files
In this step, you will learn how to use the cmp command to compare the contents of two text files.
First, let's create two text files with some differences:
echo "This is the content of file1.txt" > file1.txt
echo "This is the content of file2.txt" > file2.txt
Now, let's use the cmp command to compare the two files:
cmp file1.txt file2.txt
Example output:
file1.txt file2.txt differ: byte 25, line 1
The output shows that the two files differ at byte 25, which corresponds to the 25th character in the file. This is because the two files have different content starting from the 25th character.
You can also use the -l option to display the byte number and the differing bytes for each difference:
cmp -l file1.txt file2.txt
Example output:
25 164 163
This output indicates that the files differ at byte 25, where the bytes are 164 and 163 (in octal).
If the files are identical, the cmp command will not output anything, as the -s (silent) option is the default behavior when the files are the same.
echo "This is the content of file3.txt" > file3.txt
cmp file1.txt file3.txt
No output means the files are identical.
Compare Binary Files
In this step, you will learn how to use the cmp command to compare the contents of two binary files.
Let's start by creating two binary files with some random data:
dd if=/dev/urandom of=file1.bin bs=1024 count=1
dd if=/dev/urandom of=file2.bin bs=1024 count=1
The dd command creates two binary files, file1.bin and file2.bin, each with 1024 bytes of random data.
Now, let's use the cmp command to compare the two binary files:
cmp file1.bin file2.bin
Example output:
file1.bin file2.bin differ: byte 1, line 1
The output shows that the two files differ at byte 1, which means the first byte in the files is different.
You can also use the -l option to display the byte number and the differing bytes for each difference:
cmp -l file1.bin file2.bin
Example output:
1 302 5
This output indicates that the files differ at byte 1, where the bytes are 302 and 5 (in octal).
If the files are identical, the cmp command will not output anything, as the -s (silent) option is the default behavior when the files are the same.
dd if=/dev/urandom of=file3.bin bs=1024 count=1
cmp file1.bin file3.bin
No output means the files are identical.
Summary
In this lab, you learned about the cmp command in Linux, which is used to compare the contents of two files. You started by understanding the basic usage of the cmp command, where it compares two files byte-by-byte and reports the first location where the files differ. You then practiced comparing both text files and binary files using the cmp command, and explored various options to customize the comparison, such as displaying the byte number and differing bytes, and ignoring differences in case when comparing text files.



