Generate and Verify MD5 Hash of File in Linux

Generate and Verify MD5 Hash of File in Linux

MD5 is a cryptographic hash function that can be used for verifying file integrity. For example, to check that the file has not been modified during transfer over the network. This tutorial shows how to generate and verify MD5 hash of file in Linux.

Generate MD5 hash

Create a new file for testing:

printf 'Hello world' > test.txt

The md5sum command can be used for generating and verifying MD5 hash. To generate MD5 hash of file and print result in the terminal, run the following command:

md5sum test.txt

Output:

3e25960a79dbc69b674cd4ec67a72c62  test.txt

In order to write result to a file, use > redirection operator:

md5sum test.txt > test.md5

Verify MD5 hash

To verify file integrity, use -c option as follows:

md5sum -c test.md5

Output:

test.txt: OK

Change content of a file by appending additional text:

printf 'Hi' >> test.txt

After verifying MD5 hash of a file again, we get output:

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

Leave a Comment

Cancel reply

Your email address will not be published.