There are times when we download a file (say an ISO image) hosted somewhere on the Internet only to find that it's not working as expected (or, at all). There could be multiple reasons behind this, with one among them being file corruption (the file got corrupted during the download process, or the original, hosted file itself was corrupt). But how to confirm that such a corruption has occurred?
Well, there does exist a solution to this problem. In most cases, what's done is, when the file is originally created, a checksum is computed which is unique to that file. Even if there's a slight change in the file, the checksum - when computed again - changes.
So most vendors offer a checksum (or a checksum-like code) corresponding to the file(s) being downloaded. If the file doesn't behave in expected way, user's can recompute the file's checksum and compare it with the original checksum provided by the vendor to see if the file is intact or got corrupted.
In Linux, there's a command line tool that you can use to create/verify checksum. It's dubbed cksum. In this this tutorial, we will quickly discuss how the tool works.
Linux cksum command
This command computes a Cyclic Redundancy Check (CRC) checksum of the input file, and prints that number/code in its output on the command line terminal. In addition to checksum, the tool also shows the number of bytes the file contains.
Following is the generic syntax of the cksum command:
cksum [OPTION]... [FILE]...
Here's what the tool's official documentation says about it:
`cksum' is typically used to ensure that files transferred by unreliable means (e.g., netnews) have not been corrupted, by comparing the `cksum' output for the received files with the `cksum' output for the original files (typically given in the distribution).
How to use cksum?
The usage of this tool is pretty simple: just pass the file name as input, and the command will print the corresponding checksum as well as the number of bytes that are there in the file.
For example, we executed the following command in our case:
Here's the above command in action:
So the first number in the output is the checksum, the second number is the number of bytes, and third entity - needless to say - is the input file name.
Next, what we did was, we made a slight change in the file text, and executed the same command again:
So as you can see, the checksum got changed, indicating that there's has been some change in the file.
That's pretty much it about this tool. It does offer a couple of options, but those are pretty generic (--help and --version) - they are not only self-explanatory, but can also be found in almost every command's option list in Linux.
The cksum command is simple to understand and even simpler to use. You can easily try it on your system - if there's a file for which the corresponding CRC checksum is known, this tool can be used to verify that the file is intact. For more information on cksum, head to the tool's info page (run the command: info coreutils cksum), instead of its man page.