‘cksum’: Print CRC checksum and byte counts

‘cksum’ computes a cyclic redundancy check (CRC) checksum for each given FILE, or standard input if none are given or for a FILE of ‘-‘.

     cksum [OPTION]… [FILE]…

‘cksum’ prints the CRC checksum for each file along with the number of bytes in the file, and the file name unless no arguments were given.

‘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).

The CRC algorithm is specified by the POSIX standard.  It is not compatible with the BSD or System V ‘sum’ algorithms ; it is more robust.

The only options are ‘–help’ and ‘–version’.

An exit status of zero indicates success, and a nonzero value indicates failure.


cksum command examples


1. A basic example

On a very basic level, the cksum command can be used to display the checksum for a file.

$ cksum testfile.txt
3000792507 3 testfile.txt

The first value (big number) in the output above is the checksum for the file, then we have the size of the file and finally the name of the file.

2. Checksum changes with change in content

The test file ‘testfile.txt’ has following contents:

$ cat testfile.txt

To calculate the checksum of the test file, pass it as argument to the cksum command :

$ cksum testfile.txt
3000792507 3 testfile.txt

Now, Modify the contents of file :

$ cat testfile.txt
Hi everybody.

Again pass the test file as argument to cksum command :

$ cksum testfile.txt
2559130041 14 testfile.txt

So we see that with change in contents, the checksum changes.

3. Change in content does not always mean increase or decrease in size

Well the above is true fundamentally also and even for chksum too. Lets see what it means :

Check the contents of the test file ‘testfile.txt’ :

$ cat testfile.txt
Hi everybody

Note the checksum :

$ cksum testfile.txt
2559130041 14 testfile.txt

Now, change the content by not actually adding or deleting something but by replacing one character with other so that size of the file remains same.

$ cat testfile.txt
Hi everybudy.

So as you can see, I replaced ‘o’ with ‘u’.

Compare the checksum now:

$ cksum testfile.txt
3252191934 14 testfile.txt

So we see that the checksum changed even if the change was of one character replaced by other.

4. An interrupted copy

Suppose you are copying a zipped folder containing various sub-folders and files from one location to another and due to any reason whatsoever the copy process got interrupted, so how would you check whether everything was copied successfully or not? Well, cksum makes it possible as now we know that in case of the partial copy, the overall checksum of the destination would differ from that of the source folder.

You can simulate this scenario in the following way:

I created Linux.tar.gz and Linux_1.tar.gz from the same ‘Linux’ folder. The difference being that Linux_1.tar.gz was made when ‘Linux’ folder contained an extra text file.

So the above scenario simulates when Linux_1.tar.gz was being copied but got interrupted when just one text file was left to be copied in the target Linux.tar.gz

Now when I compare the checksum of both these files, I see

$ cksum Linux.tar.gz
756656601 1037079 Linux.tar.gz

$ cksum Linux_1.tar.gz
2598429125 1037184 Linux_1.tar.gz

So the above output shows different checksum values suggesting incorrect copy of file.

5. Checksum of standard output

This command provides a feature where-in the user can type just ‘cksum’ or ‘cksum-‘ and write on stdin and then press Ctrl+D couple of times. This way cksum gives the checksum of the data entered at the input.

$ cksum
Lets check the checksum1135634677 23

In the example above, we actually calculated the checksum of the string “Lets check the checksum”.



I have started unixadminschool.com ( aka gurkulindia.com) in 2009 as my own personal reference blog, and later sometime i have realized that my leanings might be helpful for other unixadmins if I manage my knowledge-base in more user friendly format. And the result is today's' unixadminschool.com. You can connect me at - https://www.linkedin.com/in/unixadminschool/

What is in your mind, about this post ? Leave a Reply

  Our next learning article is ready, subscribe it in your email

What is your Learning Goal for Next Six Months ? Talk to us