cmp Linux Command Guide
The Linux cmp command is used to compare two files byte by byte and display the differences between them. It is useful for verifying that two files are identical or identifying discrepancies between them. cmp is particularly handy when comparing binary files. When files are the same, cmp doesn’t display any output. If differences are found, cmp shows the byte and line numbers where the first difference occurs. By default, cmp stops at the first difference it encounters.
cmp Syntax:
Options:
Option | Description |
---|---|
-b | Show the differing bytes |
-i NUM | Skip NUM bytes from the start |
-l | Output the byte number and the differing byte (octal) |
-s | Silent mode |
-v | Output version information |
Parameters:
Parameter | Description |
---|---|
file1 | The path to the first file |
file2 | The path to the second file |
cmp Command Samples:
Compare Two Files
Compares the contents of file1.txt
and file2.txt
and displays the byte and line at which they differ, if any.
Suppress Output
Compares the files file1.txt
and file2.txt
silently. No output will be displayed if the files are identical.
Ignore the First N Bytes
Compares the files file1.txt
and file2.txt
after skipping the first N bytes in each file.
Display Differences in Detail
Compares two files and displays the differences in decimal byte-by-byte format along with the line number.
Compare Binary Files
Compares two binary files binary1.bin
and binary2.bin
for differences. Bytes are displayed in octal format.
Specify a Limit for Differences
Compares the first 1000 bytes of file1.txt
and file2.txt
and indicates the first byte at which they differ.
Check Equal Files
Checks if the files file1.txt
and file2.txt
are equal without comparing any bytes. This is a quick equality check.
cmp FAQ:
How do I use cmp in Linux?
To use the cmp command in Linux, execute the following command:
What is the purpose of the cmp command in Linux?
The cmp command in Linux is used to compare two files byte by byte and display the differences between them.
How can I suppress the output of the cmp command in Linux?
To suppress the output of the cmp command in Linux, you can use the -s
or --silent
option.
How do I show the line number and byte number where the first difference occurs in Linux using cmp?
To display the line number and byte number where the first difference occurs while using cmp in Linux, you can use the -l
or --verbose
option.
Can cmp command be used to compare binary files in Linux?
Yes, the cmp command in Linux can be used to compare both text and binary files, byte by byte.
How do I ignore the first N bytes of data while using the cmp command in Linux?
To ignore the first N bytes of data while using the cmp command in Linux, you can use the -i N
or --ignore-initial=N
option.
Applications of the cmp command
- Compare two files byte by byte
- Determine whether two files are identical or not
- Find the first byte at which the files differ
- Display the differing bytes and their offsets in the files
- Useful for verifying the integrity of files or backups