How to use the command 'cmp' (with examples)
The cmp
command is a useful utility in Unix-based systems that allows you to compare two files byte by byte. It primarily checks for binary differences, but it can be used on any files. This practical functionality makes cmp
an essential tool for developers, system administrators, and anyone needing to verify file integrity or confirm that two files are identical. Its ability to pinpoint where differences occur can be vital for troubleshooting and ensuring the accuracy of file content.
Use case 1: Output char and line number of the first difference between two files
Code:
cmp path/to/file1 path/to/file2
Motivation:
In many scenarios, you might want to quickly verify whether two files are identical or not. This basic usage of the cmp
command provides the fastest and simplest way to check for differences. By identifying the first difference, you can focus quickly on discrepancies without delving into every subsequent byte, saving time and effort.
Explanation:
cmp
: Invokes thecmp
command.path/to/file1
andpath/to/file2
: These are the file paths of the two files you want to compare. They should be specified to point directly to where the files reside on your system.
Example Output:
path/to/file1 path/to/file2 differ: byte 10, line 1
This output indicates that the first difference between the files occurs at byte 10 on line 1.
Use case 2: Output info of the first difference: char, line number, bytes, and values
Code:
cmp --print-bytes path/to/file1 path/to/file2
Motivation: When differences between files exist, knowing the exact bytes and their content can be crucial for analysis and debugging. This command provides detailed information, including the actual differing byte values, enabling a deeper understanding of the differences. This can be especially critical when working with binary files or encoded content.
Explanation:
cmp
: Invokes thecmp
command.--print-bytes
: An option that tellscmp
to display the differing bytes’ values. This is essential for scrutinizing the precise nature of the discrepancy.path/to/file1
andpath/to/file2
: These are the two files being compared.
Example Output:
path/to/file1 path/to/file2 differ: byte 10, line 1 is 72 H 101 e
The output shows that at byte 10 on line 1, the two files differ, displaying the differing byte values in both files.
Use case 3: Output the byte numbers and values of every difference
Code:
cmp --verbose path/to/file1 path/to/file2
Motivation: When you need to perform an exhaustive comparison of two files, especially large ones, knowing every differing byte number and value is valuable. This use case circumvents the need for multiple comparison runs, catering especially to those needing to log differences or perform thorough audits.
Explanation:
cmp
: Thecmp
command initiates the comparison process.--verbose
: This option is employed to makecmp
output detailed information about every difference it finds, rather than stopping after the first one.path/to/file1
andpath/to/file2
: The specific files being compared.
Example Output:
61 72
byte 10 differs, 72 vs 101
70 32
byte 23 differs, 65 vs 67
Here, the output delineates the differing byte numbers and the differing values at those points.
Use case 4: Compare files but output nothing, yield only the exit status
Code:
cmp --quiet path/to/file1 path/to/file2
Motivation:
In certain applications, especially in scripts, it might be more important to simply know whether two files differ, without needing the specifics. The --quiet
mode provides this functionality by suppressing all output, thus allowing for automation where an exit status can signify identical files (exit status 0) or differing files (exit status 1).
Explanation:
cmp
: The command being executed to perform the comparison.--quiet
: This option tellscmp
not to produce any output on the console. The focus is on the exit status for scripting and automation purposes.path/to/file1
andpath/to/file2
: The files being compared for differences.
Example Output: There is no visible output from this command, as the purpose is to confirm if the files are the same without seeing the details. The useful result is the exit status, which can be queried programmatically to make decisions in scripts.
Conclusion
The cmp
command provides a straightforward yet versatile set of functionalities for file comparison in Unix-like operating systems. From quick checks to detailed byte-by-byte analysis, cmp
caters to a variety of use cases efficiently. Whether you need to identify the first discrepancy or audit every difference, cmp
enables you to do so effectively with simple command-line instructions.