cmp

file managementLinux/Unix
The cmp command is one of the most frequently used commands in Linux/Unix-like operating systems. cmp Compare two files byte by byte

Quick Reference

Command Name:

cmp

Category:

file management

Platform:

Linux/Unix

Basic Usage:

cmp [options] [arguments]

Common Use Cases

  • 1

    File comparison

    Compare two files byte by byte for differences

  • 2

    Data validation

    Verify that files are identical or detect differences

  • 3

    Quality assurance

    Ensure file integrity and consistency

  • 4

    Script automation

    Automate file comparison in scripts and workflows

Syntax

cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]

Options

Option Description
-b, --print-bytes Print differing bytes as ASCII
-i, --ignore-initial=SKIP Skip first SKIP bytes of both inputs
-i, --ignore-initial=SKIP1:SKIP2 Skip first SKIP1 bytes of FILE1 and first SKIP2 bytes of FILE2
-l, --verbose Output byte numbers and differing byte values
-n, --bytes=LIMIT Compare at most LIMIT bytes
-s, --quiet, --silent Suppress all normal output, only return exit status
--help Display help message and exit
--version Output version information and exit

Examples

How to Use These Examples

The examples below show common ways to use the cmp command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

Basic Examples:

Compare two files
cmp file1.txt file2.txt
Compare silently (only exit status)
cmp -s file1.txt file2.txt
Compare with verbose output showing all differences
cmp -l file1.txt file2.txt
Compare and print byte numbers in decimal
cmp --print-bytes file1.txt file2.txt

Advanced Examples:

Compare two files but skip the first 10 bytes of each
cmp file1.txt file2.txt 10 10
Compare standard input with a file
cat file1.txt | cmp - file2.txt
Use in a script to check if files are different
cmp -s file1.txt file2.txt
if [ $? -ne 0 ]; then echo "Files are different" else echo "Files are identical" fi
Compare specific parts of files (skip first 512 bytes of file1)
cmp file1.bin file2.bin 512

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

The cmp command compares two files byte by byte and reports the position of the first difference. Unlike diff, which shows all differences between text files in a human-readable format, cmp performs a binary comparison and is more suitable for comparing non-text files.

Key points about cmp:

  • By default, it reports only the first difference found
  • With the -l option, it reports all differences
  • If no differences are found, cmp exits silently with status 0
  • If differences are found, the exit status is 1
  • If an error occurs, the exit status is > 1
  • It can compare standard input by using - as the filename
  • You can skip bytes at the beginning of either file for partial comparisons

Exit status codes:

  • 0: The files are identical
  • 1: The files differ
  • 2: An error occurred (inaccessible file, etc.)

Common use cases:

  • Checking if binary files are identical
  • Quick verification of file copies
  • Comparing specific portions of files
  • Shell scripts that need to determine if files match

While diff is preferred for text files and provides more detailed information about differences, cmp is often faster and more appropriate for binary files or when you only need to know if files differ but not how they differ.

Common Use Cases

File comparison

Compare two files byte by byte for differences

Data validation

Verify that files are identical or detect differences

Quality assurance

Ensure file integrity and consistency

Script automation

Automate file comparison in scripts and workflows

Binary file analysis

Compare binary files for differences or corruption

Related Commands

These commands are frequently used alongside cmp or serve similar purposes:

Use Cases

1

File comparison

Compare two files byte by byte for differences

2

Data validation

Verify that files are identical or detect differences

3

Quality assurance

Ensure file integrity and consistency

4

Script automation

Automate file comparison in scripts and workflows

5

Binary file analysis

Compare binary files for differences or corruption

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the cmp command works in different scenarios.

$ cmp
View All Commands