When managing backups or analyzing massive server logs in Linux, it is crucial to ensure that two files are completely identical before deleting one of them to save space. If these files are heavily compressed using the legacy lzma (Lempel-Ziv-Markov chain algorithm) format, the standard cmp command will only compare the binary compression headers, not the actual text inside. To perform a true byte-by-byte comparison of the actual data hidden inside the archives, you must use the lzcmp command.
How the lzcmp Command Works
The lzcmp utility acts as an intelligent bridge between the lzma decompression algorithm and the standard cmp tool. When you feed it two compressed files, it temporarily decompresses both files directly into your computer’s RAM and feeds that raw data stream into cmp, which then performs a strict byte-by-byte comparison.
To compare two compressed database dumps, you simply provide both filenames as arguments:
lzcmp backup_january.sql.lzma backup_february.sql.lzma
Understanding the Output
Because lzcmp is designed for extreme precision and automation, its default output is incredibly brief.
- If the files are identical: The terminal will output absolutely nothing. It will simply return you to a blank bash prompt. In the Unix philosophy, silence indicates absolute success.
- If the files are different: The command will immediately halt the moment it finds a discrepancy and print a single line of text stating exactly where the error occurred (e.g.,
backup_january.sql.lzma backup_february.sql.lzma differ: byte 4096, line 102).
This strict behavior makes lzcmp perfect for bash scripting. You can easily capture its exit status code in a variable: it returns 0 if the files match perfectly, 1 if they differ, and 2 if the tool encountered a fatal error (like a corrupted archive).
Comparing a Compressed File to a Plain Text File
In many administrative scenarios, you might have one file that is compressed and another file that you just created locally and haven’t compressed yet. The lzcmp command is intelligent enough to handle this mixed format seamlessly.
lzcmp current_config.txt old_config.txt.lzma
The utility will automatically recognize that current_config.txt is a plain text file, leave it alone, decompress the .lzma file in RAM, and then perform the byte-by-byte comparison flawlessly. You do not need to manually compress the text file or decompress the archive to run the check.