How to Bash md5sum Command

Sheeraz Gul Feb 02, 2024
  1. Bash md5sum Command
  2. Use md5sum on Multiple Files at Once in Bash
  3. Use md5sum to Display Modified Files in Bash
  4. Use md5sum to Identify Invalid Hash Values in Bash
  5. Get md5sum Output Without a File Name in Bash
How to Bash md5sum Command

This tutorial demonstrates how to use the md5sum command in Bash.

Bash md5sum Command

The md5sum command prints the 32-character and 128-bit checksum for the given file. This command uses the MD5 algorithm to convert the file to hash; the syntax for this command is below.

md5sum [OPTION]... [FILEPATH]...

Let’s try to run a simple md5sum command for a text file that has the following content:

Hello! this is md5sum command checking from delftstack.com

The file name is delftstack1; the md5sum command for this file will be:

md5sum delftstack1.txt

The above command will convert the file delftstack1.txt to an md5 hash. See the result:

7a09013df4a60cc5eda609d03008c547  delftstack1.txt

We can also show this output in the BSD format using the --tag option.

md5sum --tag delftstack1.txt

The output for this command will be:

MD5 (delftstack1.txt) = 7a09013df4a60cc5eda609d03008c547

The md5sum has many different options which can be used. See the table below.

Left columns Right columns
-b Used to read the result in binary mode.
-c Used to read MD5 from given files and then check them.
–tag Used to get the output in a BSD-style checksum.
-t Use to read in text mode, which is also by default.
–ignore-missing Used to ignore report status for missing files.
–quiet Used to stop printing OK for each successfully verified file.
–status Used to stop the output of anything where the status code will show success.
–strict Used to exit from non-zero for checksum files that are improperly formatted.
-w Used to warn about the checksum files which are improperly formatted.

The md5sum command can be used in different ways, which include using md5 on multiple files at once, displaying the modified files only, and identifying the invalid hash values.

Use md5sum on Multiple Files at Once in Bash

The md5sum can also be used to validate multiple files simultaneously. Let’s create two more text files and then try to validate all three files at once.

delftstack2.txt:

Hello! this is md5sum command checking from delftstack.com file 2

delftstack3.txt:

Hello! this is md5sum command checking from delftstack.com file 3

Now the command to get hash for multiple files at once is:

md5sum delftstack1.txt delftstack2.txt delftstack3.txt > hashes

The above command will only convert files to hash and not show output. To show the output, we need to run the following command:

md5sum --check hashes

The above command will show whether the files are successfully converted to a hash. See the output:

delftstack1.txt: OK
delftstack2.txt: OK
delftstack3.txt: OK

Use md5sum to Display Modified Files in Bash

The md5sum command can also display the modified files while applying the md5sum on multiple files. First of all, to modify the file, use the following command:

echo "!" >> delftstack1.txt

The above command will modify the file delftstack1.txt. Now let’s display the modified files using the md5sum options.

See the command:

md5sum --quiet --check hashes

The above command will look for modified files and print the name in the output. See the output:

delftstack1.txt: FAILED
md5sum: WARNING: 1 computed checksum did NOT match

Use md5sum to Identify Invalid Hash Values in Bash

We can also use the md5sum command with options to identify the invalid files. For this, we use the -warn option and the sed command to insert extra characters to make a file invalid.

See the first command:

sed -i '1s/.*/a&/' hashes

The above command will add extract characters to the first line of the output. See the output for this command:

sed: -e expression #1, char 2: extra characters after command

Now let’s check for the invalid hash value using the md5sum command with --warn options. See the command:

md5sum --warn --check hashes

The above will show the file with invalid hash values in the output. See the output:

delftstack1.txt: FAILED
delftstack2.txt: OK
delftstack3.txt: OK
md5sum: WARNING: 1 computed checksum did NOT match

Get md5sum Output Without a File Name in Bash

As we can see, the md5sum returns the hash output with its file name, but sometimes it is required to get the output without a file name so we can further use it. The solution to this is the awk command, a domain-specific language used for text processing, data extraction, and reporting tools.

Follow the following points to get the output without a file name in Bash:

  1. We will assign the output to a variable.
  2. First, we run the md5sum on the given file, use the awk command, and print $1.

See the command:

DemoMD5= md5sum delftstack1.txt | awk '{ print $1 }'

The above will only return the hash output from the md5sum output.

Output:

698ac7ad395a9c887b1abf3c9ded7abe

If you don’t want to use the awk command, there is another method where we can directly get the hash output from md5sum without the file name. We assign the md5sum output to an array and then print it.

See the commands:

DemoMD5=($(md5sum delftstack1.txt))
echo $DemoMD5

The above commands will also directly get the hash output without the filename for md5sum. See the output:

698ac7ad395a9c887b1abf3c9ded7abe
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook