Skip to content

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:

Terminal window
cmp [option] file1 file2

Options:

OptionDescription
-bShow the differing bytes
-i NUMSkip NUM bytes from the start
-lOutput the byte number and the differing byte (octal)
-sSilent mode
-vOutput version information

Parameters:

ParameterDescription
file1The path to the first file
file2The path to the second file

cmp Command Samples:

Compare Two Files

Terminal window
cmp file1.txt file2.txt

Compares the contents of file1.txt and file2.txt and displays the byte and line at which they differ, if any.

Suppress Output

Terminal window
cmp -s file1.txt file2.txt

Compares the files file1.txt and file2.txt silently. No output will be displayed if the files are identical.

Ignore the First N Bytes

Terminal window
cmp -i N file1.txt file2.txt

Compares the files file1.txt and file2.txt after skipping the first N bytes in each file.

Display Differences in Detail

Terminal window
cmp -l file1.txt file2.txt

Compares two files and displays the differences in decimal byte-by-byte format along with the line number.

Compare Binary Files

Terminal window
cmp -b binary1.bin binary2.bin

Compares two binary files binary1.bin and binary2.bin for differences. Bytes are displayed in octal format.

Specify a Limit for Differences

Terminal window
cmp -n 1000 file1.txt file2.txt

Compares the first 1000 bytes of file1.txt and file2.txt and indicates the first byte at which they differ.

Check Equal Files

Terminal window
cmp -n 0 file1.txt file2.txt

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:

Terminal window
cmp file1.txt file2.txt

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.

Terminal window
cmp -s file1.txt file2.txt

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.

Terminal window
cmp -l file1.txt file2.txt

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.

Terminal window
cmp -i 100 file1.txt file2.txt

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