Introduction
The md5sum command in Linux is a powerful tool for verifying the integrity of files. It is used to generate a 128-bit checksum (also known as a message digest) for a file. This checksum is a unique string of characters that can be used to verify the integrity of the file. If the checksum of a file changes, it means that the file has been modified in some way. The md5sum command is a great way to ensure that the files you download from the internet are safe and unaltered. In this guide, we will discuss the basics of how to use the md5sum command in Linux.
How to Use the md5sum Command in Linux
The md5sum command is a Linux utility used to generate and check MD5 checksums. It is commonly used to verify the integrity of files.
To generate an MD5 checksum for a file, use the following syntax:
md5sum [file]
Replace [file] with the path to the file you want to generate a checksum for.
To check the MD5 checksum of a file, use the following syntax:
md5sum -c [file]
Replace [file] with the path to the file you want to check the checksum of.
You can also generate and check multiple files at once by providing a list of files as arguments.
For more information, use the following command:
man md5sum
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.
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]
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]
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]
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]
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]
3. If you change the contents of the file and repeat the check, a warning message is displayed:
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
:
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]
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
.
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:
To display which line has an invalid hash, use -w
(--warn
):
md5sum -w -c [file-containing-hashes]
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:
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.
How to Use the md5sum Command in Linux
The md5sum command is a Linux utility used to generate and verify the MD5 (Message-Digest algorithm 5) checksum of a file. It is commonly used to verify the integrity of files that have been transferred over a network, such as when downloading a file from the internet. The md5sum command is also used to generate a checksum for a file, which can then be used to verify the file’s integrity at a later time.
How to Generate an MD5 Checksum
To generate an MD5 checksum for a file, use the following command:
md5sum [file]
Replace [file] with the name of the file you want to generate a checksum for. For example, to generate an MD5 checksum for a file named “example.txt”, use the following command:
md5sum example.txt
This will generate an MD5 checksum for the file, which will be displayed on the screen. You can then use this checksum to verify the integrity of the file at a later time.
How to Verify an MD5 Checksum
To verify an MD5 checksum, use the following command:
md5sum -c [file]
Replace [file] with the name of the file you want to verify the checksum for. For example, to verify the MD5 checksum for a file named “example.txt”, use the following command:
md5sum -c example.txt
This will compare the MD5 checksum of the file with the checksum stored in the file. If the checksums match, the command will output “OK”. If the checksums do not match, the command will output “FAILED”.
Conclusion
The md5sum command is a useful tool for verifying the integrity of files that have been transferred over a network. It can also be used to generate an MD5 checksum for a file, which can then be used to verify the file’s integrity at a later time. With the md5sum command, you can ensure that the files you download are exactly the same as the original.