JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Bash md5sum command

Author:JIYIK Last Updated:2025/03/23 Views:

This article explains how to use the md5sum command in Bash.


Bash md5sum command

md5sum command prints the 32 character and 128 bit checksum of a given file. This command converts the file into a hash using the MD5 algorithm; the syntax of this command is as follows.

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

Let's try running a simple md5sum command on a text file with the following content:

Hello! this is md5sum command checking from jiyik.com

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

md5sum jiyik1.txt

The above command will convert the file jiyik1.txt to md5 hash. View the result:

7a09013df4a60cc5eda609d03008c547  jiyik1.txt

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

md5sum --tag jiyik1.txt

The output of this command will be:

MD5 (jiyik1.txt) = 7a09013df4a60cc5eda609d03008c547

There are many different options that can be used with md5sum. See the table below.

Left column Right column
-b Used to read the result in binary format.
-c Used to read MD5 from a given file and then check them.
–tag Used to obtain BSD-style checksum output.
-t For reading in text mode, which is also the default setting.
--ignore-missing Used to ignore the reporting status of missing files.
–quiet Stops printing OK for each successfully authenticated file.
--status Used to stop the output of anything where the status code shows success.
–strict Used to exit with a non-zero value on malformed checksum files.
-w Used to warn about malformed checksum files.

md5sumThe command can be used in different ways, including using md5 on multiple files at once, showing only modified files, and identifying invalid hashes.


Using md5sum on multiple files at once in Bash

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

jiyik2.txt:

Hello! this is md5sum command checking from jiyik.com file 2
jiyik3.txt:

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

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

md5sum jiyik1.txt jiyik2.txt jiyik3.txt > hashes

The above command will only convert the file into hash without displaying the output. To display the output, we need to run the following command:

md5sum --check hashes

The command above will show you whether the file was successfully converted to a hash. See the output:

jiyik1.txt: OK
jiyik2.txt: OK
jiyik3.txt: OK

Display Modified Files Using md5sum in Bash

md5sumThe command can also display the modified files when applying md5sum to multiple files. First, to modify the files, use the following command:

echo "!" >> jiyik1.txt

The above command will modify the file jiyik1.txt. Now let us display the modified file using md5sum option.

See the command:

md5sum --quiet --check hashes

The above command will find the modified files and print the names in the output. See the output:

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

Identifying Invalid Hashes in Bash Using md5sum

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

Look at the first command:

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

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

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

Now let's check for invalid hashes using the md5sum command with the --warn option. See the command:

md5sum --warn --check hashes

The above will show the files with invalid hashes in the output. See the output:

jiyik1.txt: FAILED
jiyik2.txt: OK
jiyik3.txt: OK
md5sum: WARNING: 1 computed checksum did NOT match

Get md5sum output without filename in Bash

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

Follow the points below to get output without filename in Bash:

We assign the output to a variable.

First, we run md5sum on the given file using the awk command and print $1.

See the command:

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

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

Output:

698ac7ad395a9c887b1abf3c9ded7abe

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

View the command:

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

The above command will also directly get the hash output md5sum without the file name. View the output:

698ac7ad395a9c887b1abf3c9ded7abe

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Creating a Progress Bar in Bash

Publish Date:2025/03/23 Views:94 Category:OPERATING SYSTEM

A progress bar is a visual indicator that shows the progress of a task, such as a long-running script or command. It can be used to provide feedback to the user about the status of a task and can also help estimate the time remaining before

Sorting Arrays in Bash

Publish Date:2025/03/23 Views:73 Category:OPERATING SYSTEM

Sorting an array is a very common task in any programming language. In Bash scripting, we can also accomplish this task in two different ways. The first one uses any sorting algorithm and the second one uses a built-in keyword in Bash scrip

Multidimensional arrays in Bash

Publish Date:2025/03/23 Views:68 Category:OPERATING SYSTEM

Multidimensional array is a very important element for any program. It is mainly used to create table view of data and many other purposes. This article demonstrates how to create a two-dimensional array. In addition, we will discuss the to

String comparison in batch files

Publish Date:2025/03/22 Views:168 Category:OPERATING SYSTEM

A string is an ordered collection of characters. You can compare strings using conditional commands in batch files, namely, if, if-else, and for commands. Strings may contain spaces and special characters, which can cause errors in the batc

Remove double quotes from variable in batch file

Publish Date:2025/03/22 Views:177 Category:OPERATING SYSTEM

In batch files, variables containing multiple words or spaces must be placed in double quotes, but sometimes, we do not want to see these quotes in the output. These quotes can be removed from the variables in batch files. There are many wa

Reading file into variable in batch script

Publish Date:2025/03/22 Views:121 Category:OPERATING SYSTEM

Sometimes, we need to put the entire contents of a file into a variable for various purposes, such as finding specific data from a file, replacing a specific part of a file, etc. In Batch, it is very easy to put the entire file contents in

Print a file after skipping first X lines in Bash

Publish Date:2025/03/22 Views:187 Category:OPERATING SYSTEM

Suppose you have a file, a large file, and you want to display its contents. How would you do it? You obviously don't want to print out the entire contents of the file, as that's not very practical. You might want to print some selective li

Get the Primary IP Address in Linux

Publish Date:2025/03/22 Views:70 Category:OPERATING SYSTEM

There are various ways to get network details in Linux. We will learn some of them in this article. This simple guide is all about using different commands that can be used to get the primary IP address in Linux operating system using Bash

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial