cksum Command in Linux



cksum is a command used in Linux that helps you verify file integrity by calculating a Cyclic Redundancy Check (CRC) value and displaying the byte count of a file. The cksum command is useful for detecting corrupted downloads or file transfers. For example, when downloading a Linux ISO file, you can compare its checksum value with one generated using cksum.

The official checksum value of the ISO file can be found on the developers website, if the checksum value matches, the file is likely intact.

Table of Contents

Here is a comprehensive guide to the options available with the cksum command −

How to Install cksum Command in Linux?

The cksum command is included in the coreutils package, which is preinstalled on all Linux distributions. If you encounter command not found error while running the cksum command, you can reinstall the coreutils package from your Linux package manager.

For Linux systems like Debian, Ubuntu, or Kali Linux, the coreutils package can be reinstalled from the below-given apt command −

sudo apt install --reinstall coreutils 

For other distributions, such as RHEL, CentOS and Fedora, the coreutils package can be reinstall using the below-provided yum command −

sudo yum install --reinstall coreutils 

Once, you install the coreutils package, you can confirm the cksum installation by running the below-given command −

cksum --version
Install cksum Command in Linux

Syntax of cksum Command

The basic syntax for cksum command in Linux is as follows −

cksum filename

Replace filename with the actual name of the file you want to check. The cksum command will compute the CRC value and display the byte count for that file.

cksum Command Options

There are different options or flags you can use with the cksum command, these are discussed in the table given below −

Option Description
-a, --algorithm=TYPE This option allows you to choose the digest type (hash algorithm) to use. You can specify the desired algorithm, such as SHA-256 or SHA-512
--base64 When this flag is used, the command emits base64-encoded digests instead of the usual hexadecimal format.
-c, --check With this option, you can read checksum from files and verify them against the provided checksum values.
-l, --length=BTS Specify the desired digest length in bits.
--raw When this flag is used, the command emits raw binary digests instead of hexadecimal representation.
--tag The default behavior creates a BSD-style checksum. You can use this option to explicitly request that style.
--untagged This option creates a reverse style checksum without including the digest type.
-z, --zero This option will end lines with NULL characters and disable file name escaping.
-w, --warn This option will warn about improperly formatted check sum lines.
--ignore-missing Dont fail or report status for missing files.
--status It doesnt produce any visible output. Instead, it relies on the exit status code to indicate whether the operation was successful.
--quiet Suppress printing OK for each successfully verified file.
--strict Exit with a non-zero status for improperly formatted checksum lines.
--debug Indicate which implementation is used.
--version Output version information and exist.
--help Display help information and exist.

Examples of cksum Command in Linux

Lets discuss a few examples of cksum command that you can use on your Linux system −

  • Single File Check
  • Multiple Files Check
  • Recursive Check for All Files in a Directory
  • Redirect Output to a File
  • Calculate Algorithm Checksum for a File

Single File Check

One of the basic examples of cksum command on Linux is to check the CRC value of a single file. You can do this by providing the name of the file with the cksum command. For example, to check for a file named sample.ppm, run the below-given command −

cksum sample.ppm
Single File Check

The cksum command will print the CRC value and file size in bytes for your specified file.

Multiple Files Check

If you want to check the CRC value of multiple files all at once, you can use the cksum command followed by the name of the files with space between their names. For example, the following command will check the checksum values of two files named sample.ppm and sample.jpg

cksum sample.ppm sample.jpg
Multiple Files Check

Recursive Check for All Files in a Directory

To recursively check all files in a directory including subdirectories, you can use the following command −

find /path/to/directory -type f -exec cksum {} \;

Replace /path/to/directory with the actual path to the directory you want to check. This command will calculate the checksum for each file within that directory structure.

Recursive Check All Files in Directory

Redirect Output to a File

You can also modify the cksum command in such a way that the checksum values of the files will be redirected to another file as an output. This can be done by using the redirection operator. For example, the following command will redirect the checksum value of a file named sample.ppm into another file called checksum.txt

cksum sample.ppm > checksums.txt
Redirect Output to a File

Calculate Algorithm Checksum for a File

You can calculate the algorithm checksum for a file by specifying the desired cryptographic hash function using the --algorithm option. For instance, to calculate the SHA-256 checksum for a file named sample.ppm, you can use the following command −

cksum --algorithm=sha256 sample.ppm
Calculate Algorithm Checksum for a File

The output will display the SHA-256 hash value for the file.

Thats how you can use the cksum command to calculate the checksum value for a file.

Conclusion

cksum is a useful Linux command that helps you calculate checksums, verify file integrity, and detect any changes or corruption. Whether you need CRC values, base64-encoded digests, or customized output formats, cksum provides flexibility for various use cases.

This tutorial covered the syntax of cksum command along with different options to be used with the command. The examples provided later on are straightforward and can help you understand the commands basics.

Advertisements