Logo

md5sum Command in Linux with 11 Practical Examples

Try this guide with our instant dedicated server for as low as 40 Euros

md5sum command in linux

In these days of rising cyber security issues, downloading a file from the internet is a risky move as there is a high chance that it may have been tampered with and can cause security challenges for your system. 

md5sum, the digital fingerprint maker of the Linux world is a cryptographic detective that can help you detect if a file is tampered with or not. 

Whether you’re safeguarding critical data, verifying downloaded files, or ensuring the integrity of backups, md5sum is an indispensable tool in your Linux toolkit that ensures the file is not altered or tampered with in any way.

In this tutorial, we will discuss md5sum command and some of its use cases. We will start with an introduction to the md5sum and then go into the use cases.

Table Of Contents

  1. What is md5sum?
    1. The Prerequisites to Working with md5sum
  2. Highlighting the md5sum Command with 10 Examples
    1. Use Case #1: Generate MD5 Hash
    2. Use Case #2: Validate md5 Checksum with a File
    3. Use Case #3: Validate Multiple Files
    4. Use Case #4: Read in Text Mode
    5. Use Case #5: Read in Binary Mode
    6. Use Case #6: Create a BSD-Style Checksum
    7. Use Case #7: Display Only the Modified Files
    8. Use Case #8: Generate Status Only
    9. Use Case #9: Check Improperly Formatted Checksum Lines
    10. Use Case #10: Skip Reporting Status for Missing Files
    11. Use Case #11: Show Help and Version Information
  3. Conclusion
  4. FAQs

What is md5sum?

md5sum in Linux is a command line utility that computes and verifies 128-bit MD5 hashes, also known as checksums or hash values, for files.

MD5 (Message Digest Algorithm 5) generates a 32-character hexadecimal string unique to the input data. 

The primary function of md5sum is to create a unique hash for a given file that serves as a digital fingerprint, uniquely representing the file’s contents and confirming the integrity of the file..

The Basic Syntax

The basic syntax of md5sum command is as follows:

# md5sum [OPTION]... FILE...

Here,

[OPTION]…: These flags modify the behavior of the command. 

FILE…: This represents the file(s) for which you want to generate the MD5 hash. You can specify multiple files by separating them with spaces.

Let us now discuss some of the options in the  md5sum command.

Options

The following table summarizes some of the common options we recommend using with the md5sum command.

md5sum command options

Now that you have a working understanding of md5sum, let us discuss some of the use cases where you can use the utility to verify files and downloads. However, before that, let us take a quick look at the prerequisites. 

The Prerequisites to Working with md5sum 

Before trying out the use case we will discuss in a while, make sure you have the following:

  • A Linux-based system
  • A user account with sudo or administrative privilege.

Highlighting the md5sum Command with 10 Examples

The md5sum command is a tool in Linux that verifies the integrity of downloaded files. It calculates a unique fingerprint, called a hash, using the MD5 (Message-Digest Algorithm 5) hashing function.  

Here are several use cases where you can see the md5sum command in action.

Use Case #1: Generate MD5 Hash

By default, md5sum treats files as text. 

As a result, you can execute the basic md5sum command with the filename to generate the hash. This unique identifier acts as a unique fingerprint of the data. Other system applications and users can then use this hash to verify the integrity of the file contents.

The syntax of the command is as follows:

# md5sum [filename]

Generate MD5 Hash

The output produces the MD5 hash followed by the filename.

Important: The md5sum can be used to check whether a file you downloaded is intact, but only if you trust the website. Hackers could alter both the file and its checksum if they gain access to a website, making it appear as if the file is safe when, in fact, it is not.

Use Case #2: Validate md5 Checksum with a File

Let us consider the above example where we generated an MD5 hash.

Now that you have the MD5 hash, verify the checksum with a file. In order to do this, generate a hash file and then verify the hashes.

Generate a Hash File

To generate a hash file, run md5sum with the > operator to redirect the output (containing the filename and hash) to a file.

# md5sum [filename] > [file-containing-hashes]

The output generates a hash file.

Verify the Hashes

Now let us verify the generated hash using the -c option to check if the hash value in the file matches the current hash value of the .txt file.

# md5sum -c [file-containing-hashes]

If the hash value matches, you will see the OK in the output.

md5sum -c value match

Alternatively, if there is a mismatch, you will see the failed warning message.

md5sum -c mismatch

Use Case #3: Validate Multiple Files

If you have multiple files to check, generate checksums for multiple files by executing the following command syntax:

# md5sum [filename1] [filename2] > [file-containing-hashes]

Next, verify the integrity of multiple files using -c flag.

# md5sum -c [file-containing-hashes]

The output shows both success and failure for the hash values in the target file.

Validate Multiple Files

Use Case #4: Read in Text Mode

By default, md5sum reads files in binary mode. However, you can change this default behavior and read the file in text mode by using the -t option.

# md5sum -t [filename]

Read in Text Mode

Use Case #5: Read in Binary Mode

Opening a file in binary mode is crucial in various scenarios where handling the file’s data exactly as it is stored on disk is a critical operational necessity. 

This is primarily essential when working with non-text data to ensure data integrity and consistency across different environments. 

It also prevents unwanted modifications that may occur when the utility reads files in the text mode and guarantees that the data is processed exactly as intended.

To open the file in binary mode, use the -b (or –binary) option.

# md5sum -b [filename]

Read in Binary Mode

The * symbol before the file name indicates that md5sum reads it in binary mode.

Use Case #6: Create a BSD-Style Checksum

BSD-Style checksums are ideal for quick integrity checks, especially in environments where performance is a concern.

We recommend using the –tag option to display the hash value in the BSD-style format.

# md5sum --tag [filename]

Create a BSD-Style Checksum

Use Case #7: Display Only the Modified Files

In general, md5sum reads all files, regardless of when a file is modified. If you have a large number of files, you can imagine the time required to parse them through the utility.

If you prefer to display files with changed hash values, use the –quiet option. This command syntax lists files that are modified or tampered with.

# md5sum --quiet -c [file-containing-hashes]

Display Only the Modified Files

Use Case #8: Generate Status Only

The md5sum command, when used with the –status option, doesn’t display any messages on the screen. Instead, it returns a code: 0 if there are no changes in the hash values and 1 if changes are detected. 

This output is handy for scripts where you don’t need regular more detailed output.

The following script demonstrates the –status option.

#!/bin/bash

md5sum --status -c hashfile

Status=$?

echo "File check status is: $Status"

exit $Status

If the output of script shows status as 1, it indicates the file has been modified.

Generate Status Only

Use Case #9: Check Improperly Formatted Checksum Lines

Using the –strict option will exit the file with a non-zero status when improperly formatted hashes are encountered.

# md5sum --strict -c [file-containing-hashes]

md5sum --strict -c

To identify the specific line with an invalid hash, use the -w (–warn) option.

# md5sum -w -c [file-containing-hashes]

md5sum -w -c

Use Case #10: Skip Reporting Status for Missing Files

md5sum warns when it cannot find files on the system by default. To stop these warnings, use the –ignore-missing option.

# md5sum --ignore-missing -c [file-containing-hashes]

In the example below, file2.txt was removed before executing the md5sum command. The output doesn’t mention the deleted file.

Skip Reporting Status for Missing Files

Use Case #11: Show Help and Version Information

Like most Linux utilities and commands, you can view the official help documentation and version information for the md5sum command.

We recommend running the following command to get help with the various options of the utility:

# md5sum --help

Similarly, run the following command to check md5sum version:

# md5sum --version

Conclusion

md5sum command in Linux is a useful tool for verifying the integrity of files by calculating and comparing their MD5 checksums. It helps ensure data accuracy and detect any changes or corruption in files. With options like –quiet, –status, and –ignore-missing, users can customize how md5sum operates based on their specific needs. 

Whether verifying single files or multiple files at once, md5sum provides a reliable method for confirming file integrity in Linux systems.

FAQs

Q. What is MD5 hash?

The MD5 hash is a unique fixed-size string generated from data using the MD5 algorithm, often used to verify file integrity.

Q. How does md5sum verify file integrity?

md5sum calculates the MD5 hash of a file and compares it with a previously calculated hash to check if the file has been altered.

Q. How do I use md5sum to check file integrity?

Simply run md5sum filename to generate the MD5 hash of ‘filename’ and compare it with a known good hash.

Q. Can md5sum check multiple files at once?

Yes, use md5sum file1 file2 … to generate hashes for multiple files and verify them against a checksum file.

Q. What is a checksum file in md5sum?

A checksum file contains MD5 hashes for files; md5sum can be used to verify the integrity of listed files.

Q. How do I create a checksum file with md5sum?

Use md5sum * > checksum.md5 to create a file containing MD5 hashes for all files in the current directory.

Q. What does the status code of md5sum mean?

A status code of 0 means all files checked out, while 1 indicates a mismatched hash or file error.

Q. How do I ignore missing files in md5sum?

Add –ignore-missing to skip warnings when files listed in the checksum file aren’t found in the current directory.

Q. What happens if I change a file after creating its checksum?

md5sum will detect the change when you verify the file again, showing a warning or error depending on the options used.

Q. Can md5sum operate in default mode without options?

Yes, md5sum operates in default mode by calculating and displaying MD5 hashes for files listed as input.

Try this guide with our instant dedicated server for as low as 40 Euros