Home > Text Processing & Search > cmp

cmp: Compare two files byte by byte

The `cmp` command compares two files byte by byte, reporting the first differing location and line number. It is useful for quickly checking if simple binary or text files are identical.

Overview

cmp is a much simpler comparison tool than the `diff` command. While `diff` shows detailed line-by-line differences between files, `cmp` simply outputs the fact that 'the two files differ,' along with the first differing location (byte offset) and line number. Therefore, it is highly effective when you only need to check for file identity, such as with configuration files or binary files, without needing to see the entire content.

Key Features

The key features of the `cmp` command are as follows:

  • Compares files byte by byte.
  • Outputs only the first differing location and line number.
  • Simple output, suitable for checking file identity in scripts.
  • Especially useful for comparing binary files.

Differences from diff

Both `cmp` and `diff` are file comparison tools, but they differ in purpose and output method.

  • cmp: Byte-by-byte comparison, reports only the first difference, suitable for binary file comparison.
  • diff: Line-by-line comparison, reports all differences in detail, suitable for text file comparison.

Key Options

Commonly used `cmp` command options are grouped by purpose.

1) Comparison Options

2) Help

Generated command:

Try combining the commands.

Description:

`cmp` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Explore the functionality of the `cmp` command through various usage examples.

Compare Two Text Files

echo 'Hello World' > file1.txt
echo 'Hello World' > file2.txt
cmp file1.txt file2.txt

Comparing two identical text files will produce no output.

Compare Two Different Text Files

echo 'Hello World' > file1.txt
echo 'Hello, World' > file2.txt
cmp file1.txt file2.txt

If file contents differ, it outputs the byte position and line number of the first difference.

Compare Binary Files

cmp /bin/ls /bin/cmp

Simply checks if binary files are identical. No output means the files are identical.

Usage in Scripts

cmp -s file1.txt file2.txt && echo 'Files are identical.' || echo 'Files are different.'

Uses `cmp`'s exit code to branch execution based on file identity.

Exit Codes

`cmp` reports comparison results via exit codes, making it suitable for use in scripts.

`cmp` Exit Status Values
Code Meaning
0 Files are identical
1 Files differ
2 Inaccessible file or option error

Installation

`cmp` is included by default as part of the `diffutils` package in most Linux distributions. No separate installation is required.

Tips & Cautions

Here are some points to note when using the `cmp` command.

Tips

  • `cmp` is optimized for comparing binary files. For viewing content differences in text files, it is recommended to use `diff`.
  • Using the `cmp -s` option produces no output, making it convenient for use with `if` statements or `&&`/`||` operators in scripts.
  • Byte positions start from 1, and byte values output by `cmp -l` are in octal notation.

Related commands

These are commands that are functionally similar or are commonly used together.


Same category commands