Linux cksum Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the cksum command in Linux to calculate the checksum of files and verify their integrity. The cksum command generates a unique numerical value that represents the contents of a file, which can be used to ensure that the file has not been corrupted or modified. You will start by understanding the basic usage of the cksum command, then learn how to calculate checksums for single and multiple files, and finally, how to use the checksums to verify file integrity. This lab covers the essential skills of basic file and directory operations in Linux.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/wc("`Text Counting`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/PackagesandSoftwaresGroup -.-> linux/wget("`Non-interactive Downloading`") subgraph Lab Skills linux/cat -.-> lab-422605{{"`Linux cksum Command with Practical Examples`"}} linux/wc -.-> lab-422605{{"`Linux cksum Command with Practical Examples`"}} linux/echo -.-> lab-422605{{"`Linux cksum Command with Practical Examples`"}} linux/wget -.-> lab-422605{{"`Linux cksum Command with Practical Examples`"}} end

Understand the cksum Command

In this step, you will learn about the cksum command in Linux, which is used to calculate the checksum of a file. The checksum is a unique numerical value that represents the contents of a file and can be used to verify the integrity of the file.

To understand the cksum command, let's start by running it on a file:

cksum file.txt

Example output:

2995857905 12 file.txt

The output of the cksum command consists of three parts:

  1. The checksum value (2995857905 in the example)
  2. The length of the file in bytes (12 in the example)
  3. The filename (file.txt in the example)

The checksum value is a 32-bit cyclic redundancy check (CRC) value that is calculated based on the contents of the file. This value can be used to verify the integrity of the file by comparing it to the checksum of the original file.

The cksum command can also be used to calculate the checksum of multiple files at once:

cksum file1.txt file2.txt file3.txt

Example output:

2995857905 12 file1.txt
3456789012 34 file2.txt
6789012345 56 file3.txt

In this example, the cksum command calculates the checksum for each of the three files and displays the results.

Calculate Checksums for Files

In this step, you will learn how to calculate the checksum of files using the cksum command.

First, let's create a sample file to work with:

echo "This is a sample file." > sample.txt

Now, let's calculate the checksum of the sample.txt file:

cksum sample.txt

Example output:

2995857905 21 sample.txt

The output shows the checksum value (2995857905), the file size in bytes (21), and the filename (sample.txt).

You can also calculate the checksums of multiple files at once:

cksum sample.txt file1.txt file2.txt

Example output:

2995857905 21 sample.txt
3456789012 34 file1.txt
6789012345 56 file2.txt

This command calculates the checksum for each of the three files and displays the results.

The cksum command is useful for verifying the integrity of files, especially when transferring or downloading files. By comparing the checksum of the original file with the checksum of the downloaded file, you can ensure that the file has not been corrupted during the transfer.

Verify File Integrity Using cksum

In this step, you will learn how to use the cksum command to verify the integrity of files.

Let's start by creating a sample file and calculating its checksum:

echo "This is a sample file." > sample.txt
cksum sample.txt

Example output:

2995857905 21 sample.txt

Now, let's intentionally modify the file and see how the checksum changes:

echo "This is a modified sample file." > sample.txt
cksum sample.txt

Example output:

3456789012 29 sample.txt

As you can see, the checksum value has changed, indicating that the file content has been modified.

You can use this checksum verification process to ensure the integrity of files, especially when downloading or transferring files from one location to another. By comparing the checksum of the original file with the checksum of the downloaded file, you can verify that the file has not been corrupted during the transfer.

For example, let's say you have downloaded a file from the internet and want to verify its integrity:

## Download a file
wget https://example.com/file.zip

## Calculate the checksum of the downloaded file
cksum file.zip

Example output:

3456789012 1234567 file.zip

Now, you can compare this checksum value with the checksum provided by the file's publisher or the website you downloaded it from. If the checksums match, you can be confident that the file has not been tampered with and is safe to use.

Summary

In this lab, you learned about the cksum command in Linux, which is used to calculate the checksum of a file. The checksum is a unique numerical value that represents the contents of a file and can be used to verify the integrity of the file. You learned how to calculate the checksum of a single file as well as multiple files at once, and how to interpret the output of the cksum command. Additionally, you learned how to use the cksum command to verify the integrity of a file by comparing its checksum to the original checksum.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like