UNIX utilities: Which command compares two files byte-by-byte and reports the first difference (primarily for binary comparison)?

Difficulty: Easy

Correct Answer: cmp

Explanation:


Introduction / Context:
Comparing files is common in troubleshooting, backups, and build pipelines. UNIX provides multiple tools for comparison; choosing the right one depends on whether you need binary or textual diffs.



Given Data / Assumptions:

  • You need to know if two files are identical at the byte level.
  • Output should indicate the first differing location for quick diagnostics.
  • Standard UNIX tools are available.


Concept / Approach:
The cmp command compares two files byte by byte and reports the first mismatch (with offsets). This is ideal for binary equivalence checks. For textual differences line by line, diff is typically used, but diff is not listed here. The du command reports disk usage. DOS comp is not a standard UNIX tool.



Step-by-Step Solution:

Run cmp file1 file2If silent success is desired, use cmp -s file1 file2 and check exit status.Automate equivalence tests in scripts by checking the return code (0 = identical, non-zero = different).


Verification / Alternative check:
Use checksums: sha256sum file1 file2 and compare hashes; identical hashes imply identical content.



Why Other Options Are Wrong:
ccp: Not a standard UNIX comparison command. du: Reports disk usage, not content differences. comp: DOS/Windows utility, not standard on UNIX. None of the above: Incorrect because cmp is correct.



Common Pitfalls:
Expecting human-readable line diffs from cmp; for that, use diff. Comparing text files with different line endings may show differences; normalize if appropriate.



Final Answer:
cmp

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion