How to calculate the MD5 checksum of a file with Bash

The MD5 (Message Digest Algorithm 5) checksum is a hash function used to verify the integrity of a file. By comparing the MD5 checksum of an original file with that of a downloaded or copied file, we can determine whether the file has been altered or corrupted. In Linux, you can calculate the MD5 checksum of a file directly from the command line using Bash. In this article, we will see how to calculate the MD5 checksum of a file using the md5sum command in Bash and how to use it to verify the integrity of files.

On many Linux distributions, md5sum is already pre-installed. You can check if it is available by typing:


md5sum --version

If the command returns a version, md5sum is installed and you can proceed. Otherwise, you may need to install it through your distribution's package manager.

The md5sum command is very simple to use. The basic syntax is:


md5sum filename

For example, suppose we want to calculate the MD5 checksum of a file called document.txt. Open a terminal and type:


md5sum document.txt

This command will produce output similar to this:


5d41402abc4b2a76b9719d911017c592 document.txt

The value on the left (5d41402abc4b2a76b9719d911017c592) is the MD5 checksum, while the value on the right is the file name. If the file is modified, the MD5 checksum will change, making it easy to detect any alterations.

If you already have a reference MD5 checksum, you can compare it to the current file to verify that they are identical. Suppose we have the checksum saved in a text file called checksum.md5, containing:


5d41402abc4b2a76b9719d911017c592 document.txt

To verify that document.txt matches the specified checksum, use the following command:


md5sum -c checksum.md5

This command will compare the calculated MD5 checksum with the one in the checksum.md5 file. If they match, you'll see a message like this:


document.txt: OK

If the checksum doesn't match, you'll get a warning that the file is invalid or has been tampered with.

To automatically calculate and verify the MD5 checksum of multiple files, you can create a Bash script. For example, here's a script that calculates the checksum of all files in a directory and saves it to a file called checksums.md5.


#!/bin/bash

# Verify that the directory is passed as an argument
if [ -z "$1" ]; then
  echo "Usage: $0 <directory>"
  exit 1
fi

# Navigate to the specified directory
cd "$1" || exit

# Create or overwrite checksums.md5
md5sum * > checksums.md5
echo "MD5 checksums saved in checksums.md5"

Conclusion

Calculating MD5 checksums in Bash is a simple and very useful way to ensure file integrity. Using md5sum, you can both generate a file checksum and compare it to reference values ​​to ensure that files have not been altered or corrupted. With the help of a Bash script, you can also automate the generation and verification of checksums for large numbers of files.

Back to top