Skip to content

cksum command in Linux

The Linux cksum command is used to calculate a cyclic redundancy check (CRC) checksum for files. This checksum can be compared between systems to check for file integrity or changes. Additionally, cksum can also display the byte count of a file. By using the options available with cksum, users can control the output format and achieve the desired checksum calculation for their specific needs.

cksum Syntax:

Terminal window
cksum [option] [file(s)]

Linux cksum Options:

OptionDescription
-helpDisplay a help message and exit
-bTreat input as binary
-hPrint the checksum and the number of bytes in the input file

cksum Parameters:

ParameterDescription
file(s)The file(s) to calculate the checksum for

How to use cksum command:

Calculate Checksum of a File

Terminal window
cksum file.txt

Calculate the checksum of the file “file.txt”.

Calculate Checksum of Multiple Files

Terminal window
cksum file1.txt file2.txt file3.txt

Calculate the checksum of multiple files simultaneously.

Verify File Integrity with Checksum

Terminal window
cksum -c checksums.txt

Verify the integrity of files listed in the checksums.txt file.

Generate Checksum in Different Format

Terminal window
cksum -b file.txt

Generate the checksum of a file in binary format.

Display Only The Checksum Value

Terminal window
cksum -s file.txt

Display only the checksum value without filename.

Checksum for a Specific Number of Bytes

Terminal window
cksum -n 100 file.txt

Generate checksum for the first 100 bytes of the file.

Recursive Checksum Calculation

Terminal window
cksum -r folder/

Calculate checksum recursively for all files in a folder.

Ignore Linefeed Characters

Terminal window
cksum -L file.txt

Calculate checksum with ignoring linefeed characters.

How do I use cksum in Linux?

To use the cksum command in Linux, execute the following command:

Terminal window
cksum file.txt

How do I calculate a checksum for multiple files in Linux?

To calculate the checksum for multiple files in Linux, you can use the following command format:

Terminal window
cksum file1.txt file2.txt file3.txt

How can I display the checksum value only in cksum?

You can display only the checksum value in cksum by using the following command:

Terminal window
cksum -s file.txt

How do I include the checksum byte count in the cksum output?

To include the checksum byte count in the cksum output, you can use the following command:

Terminal window
cksum -o file.txt

How do I suppress the output of file names in cksum?

To suppress the output of file names in cksum and only display the checksum and byte count, use the following command:

Terminal window
cksum -q file.txt

How can I verify the integrity of a file using cksum?

To verify the integrity of a file using cksum, you can compare the generated checksum with the original checksum. Here’s an example:

Terminal window
original=$(cksum file.txt | cut -d' ' -f1)
generated=$(cksum file.txt | cut -d' ' -f1)
if [ $original == $generated ]; then
echo "Integrity verified"
else
echo "Integrity check failed"
fi

How do I generate a checksum using a string input with cksum?

To generate a checksum using a string input with cksum, you can use echo to pass the string and pipe it to cksum. Here’s an example:

Terminal window
echo "Hello, World!" | cksum

How can I recursively calculate checksums for all files in a directory in Linux?

You can recursively calculate checksums for all files in a directory in Linux using the find command in combination with xargs and cksum. Here’s an example:

Terminal window
find /path/to/directory -type f | xargs cksum

How do I calculate the checksum without checking for ASCII control characters?

To calculate the checksum without checking for ASCII control characters using cksum, you can use the following command:

Terminal window
cksum -B file.txt

Applications of the cksum command

  • Verifying data integrity
  • Checking for file corruption
  • Generating checksum values for files
  • Comparing checksum values for files
  • Detecting changes in files
  • Ensuring data consistency
  • Validating file transfer accuracy