cmp: Compare two files byte by byte

The “cmp” command is a utility commonly found in Unix-like operating systems that allows users to compare two files on a byte-by-byte basis. Its primary purpose is to determine whether two files are identical or to identify the first differing byte between them.

When you execute the “cmp” command followed by the names of two files, it reads both files sequentially and performs a byte-by-byte comparison between them. The command starts by comparing the first byte of each file and continues until it finds a mismatched byte or reaches the end of either file.

If the files are identical, “cmp” doesn’t produce any output and returns no error code. However, if a difference is detected, “cmp” displays the byte position where the first difference occurs and exits. This information helps pinpoint the location and magnitude of the discrepancy between the two files.

“cmp” can be useful in various scenarios, such as:

  • Verifying file integrity: By comparing a file against a known reference, “cmp” can determine whether any modifications have been made, ensuring data integrity.
  • Identifying changes between versions: When comparing two versions of a file, “cmp” helps highlight the exact byte(s) where modifications have occurred, allowing users to analyze and understand the changes made.
  • Checking file synchronization: “cmp” can be employed to compare two versions of a file on separate systems, verifying if they are synchronized or if any discrepancies exist.
  • Binary file comparisons: While textual file comparisons are often performed using tools like “diff,” “cmp” is valuable for comparing binary files, where byte-level differences are critical.

It’s worth noting that “cmp” works at a low level, treating files as streams of bytes without considering their content or format. If you require a more context-aware comparison, such as comparing lines in text files, using the “diff” command might be more suitable.

cmp Command Examples

1. Output char and line number of the first difference between two files:

# cmp /path/to/file1 /path/to/file2

2. Output info of the first difference: char, line number, bytes, and values:

# cmp --print-bytes /path/to/file1 /path/to/file2

3. Output the byte numbers and values of every difference:

# cmp --verbose /path/to/file1 /path/to/file2

4. Compare files but output nothing, yield only the exit status:

# cmp --quiet /path/to/file1 /path/to/file2

Summary

In summary, “cmp” is a command-line tool used to compare two files byte by byte. It detects the first differing byte and provides its position, helping users identify discrepancies and verify file integrity or synchronization.

Related Post