How to Use the md5sum Command in Linux

February 17, 2021

Introduction

When you download a file from the internet, it is a good safety practice to check whether you received the original version. Comparing checksums you received from the file creator with the ones you obtain by checking the file yourself is a reliable way to confirm your download’s integrity.

The md5sum command in Linux helps create, read, and check file checksums.

In this tutorial, you will learn how to use the md5sum command to validate the files you receive.

How to Use the md5sum Command in Linux

Prerequisites

  • A system running Linux
  • Access to the command line

The md5sum Command with Examples

When used on a file without any options, the md5sum command displays the file’s hash value alongside the filename. The syntax is:

md5sum [filename]
Checking the hash value of a file

After obtaining the hash value, compare it with the MD5 value provided by the file creator.

Note: While md5sum is a reliable method for testing whether the file you received has been compromised, it is useful only if you know that the website you downloaded it from is secure. If hackers gain access to the website, they can change both the file and its checksum, making it appear as if the file you are downloading is safe.

Read in Binary Mode

To read the file in binary mode, use the -b option (--binary):

md5sum -b [filename]
Using the -b option to read checksum in binary mode

The * character before the file name means that md5sum read it in binary mode.

Read in Text Mode

Use the -t option (--text) to read the file in text mode:

md5sum -t [filename]
Using the -t option to read checksum in text mode

Text mode is the default mode for reading files with md5sum.

Create a BSD-Style Checksum

Using the --tag option outputs the hash value in the BSD-style format:

md5sum --tag [filename]
Using the --tag option to output checksum in a BSD format

Validate md5 Checksum with a File

To check a file by comparing its hash value with the value provided in a hash file, use the -c option.

1. As an example, create a hash file containing the md5sum output:

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

2. Use the following syntax to compare the hash value from the file you created against the current hash value of the .txt file:

md5sum -c [file-containing-hashes]
Validating a file using a file containing its hash value, no failures detected

3. If you change the contents of the file and repeat the check, a warning message is displayed:

Validating a file using a file containing its hash value, failure detected

Validate Multiple Files

Use the same md5sum -c procedure to check the integrity of multiple files:

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

In the following example, the contents of example2.txt have changed, resulting in a warning message from md5sum:

Validating multiple files using a file containing their hash value, failure detected

Display Only Modified Files

The --quiet option displays only the files whose hash value has changed. It skips the output of validated files.

md5sum --quiet -c [file-containing-hashes]
Validating multiple files using a file containing their hash value, skipping the output of the validated files

Generate Status Only

The md5sum command with the --status option does not produce any output but returns 0 if there are no changes and 1 if it detects changes. This argument is useful for scripting, where there is no need for standard output.

The example script below illustrates the use of the --status option:

#!/bin/bash
md5sum --status -c hashfile
Status=$?
echo "File check status is: $Status"
exit $Status

When the script executes, it shows status 1, meaning that md5sum detected the change made earlier in example2.txt.

Executing a script illustrating md5sum's --status option

Check Improperly Formatted Checksum Lines

Add the --strict option to exit non-zero for improperly formatted hash values:

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

The example shows the output of md5sum --strict when you put invalid characters in the first line of the file containing hashes:

Using the --strict option to exit non-zero for improperly formatted hash values

To display which line has an invalid hash, use -w (--warn):

md5sum -w -c [file-containing-hashes]
Using the -w option to display which line has an invalid hash

The example above shows the -w option displaying that the improperly formatted MD5 checksum line is line 1 of the file.

Skip Reporting Status for Missing Files

By default, md5sum shows warnings about the files it cannot find on the system. To override this behavior, use the --ignore-missing option:

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

In the example below, example1.txt was deleted before running the md5sum command. The output ignores the deleted file:

Using the --ignore-missing option to ignore files that are missing from the system

Show Help and Version Information

To get the official help for the md5sum command, type:

md5sum --help

To check md5sum version, type:

md5sum --version 

Note: You should also check out our overview of the diff command to learn how to compare two files line by line.

Conclusion

After completing this tutorial, you should know how to properly use the md5sum command to create, print, or check MD5 checksums.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
Echo Command in Linux (With Examples)
May 20, 2024

The echo command prints out a text string you provide as the output message. This tutorial covers the echo...
Read more
19 Crucial Linux ls Commands to Know
July 30, 2020

The ls command (short for list) lists information about directories and any type of files in the working...
Read more
Linux Commands Cheat Sheet: With Examples
November 2, 2023

A list of all the important Linux commands in one place. Find the command you need, whenever you need it or...
Read more
How to Create a File in Linux Using Terminal/Command Line
July 11, 2024

Creating a file in Linux might seem straightforward, but there are some surprising and clever techniques. In...
Read more