Bash md5sum command
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. |
md5sum
The 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
md5sum
The 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 awk
the 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.
Related Articles
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s
Linux practice - regularly delete files under the directory
Publish Date:2025/04/08 Views:198 Category:OPERATING SYSTEM
-
Since we want to delete the files under the directory regularly, we need to use the Linux crontab command. And the content format of each work routine is also introduced in the format of each crontab work. Similarly, we need to use shell sc
How to use the Linux file remote copy command scp
Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM
-
Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u