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.
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.
- 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:
After obtaining the hash value, compare it with the MD5 value provided by the file creator.
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 (
md5sum -b [<em>filename</em>]
* character before the file name means that
md5sum read it in binary mode.
Read in Text Mode
-t option (
--text) to read the file in text mode:
md5sum -t [<em>filename</em>]
Text mode is the default mode for reading files with
Create a BSD-Style Checksum
--tag option outputs the hash value in the BSD-style format:
md5sum --tag [<em>filename</em>]
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
1. As an example, create a hash file containing the
md5sum [<em>filename</em>] > [<em>file-containing-hashes</em>]
2. Use the following syntax to compare the hash value from the file you created against the current hash value of the
md5sum -c [<em>file-containing-hashes</em>]
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 [<em>filename1</em>] [<em>filename2</em>] [<em>filename3</em>] > [<em>file-containing-hashes</em>]
In the following example, the contents of
example2.txt have changed, resulting in a warning message from
Display Only Modified Files
--quiet option displays only the files whose hash value has changed. It skips the output of validated files.
md5sum --quiet -c [<em>file-containing-hashes</em>]
Generate Status Only
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
#!/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
Check Improperly Formatted Checksum Lines
--strict option to exit non-zero for improperly formatted hash values:
md5sum --strict -c [<em>file-containing-hashes</em>]
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
md5sum -w -c [<em>file-containing-hashes</em>]
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
md5sum shows warnings about the files it cannot find on the system. To override this behavior, use the
md5sum --ignore-missing -c [<em>file-containing-hashes</em>]
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:
To check md5sum version, type:
After completing this tutorial, you should know how to properly use the
md5sum command to create, print, or check MD5 checksums.