File transmissions use checksums (also called hash) to verify the integrity of content, whether it is over internet or FTP. Checksum is an alphanumeric code generated out of the contents of a file, generally attached to the file by sender. The receiver generates checksum based on received file’s content and checks it against the attached checksum. Only if both match, it means the file has been sent correctly. If you change even one bit of the file content, it will completely change its checksum. There are different algorithms used to generate checksums, MD5 is one of them. In this article, we will look at how to generate & verify MD5 Hash of file in Linux.
How to Generate & Verify MD5 Hash of File in Linux
Here are the steps to generate and verify MD5 checksum for file. MD5 is a very powerful algorithm that returns a unique 128-bit alphanumeric checksum based on your file contents. It is nearly impossible for two different file contents to have same MD5 checksum.
Generate MD5 Checksum
By default, almost every Linux distribution contains md5sum utility that we will use to generate & verify checksum. Here is the syntax to generate MD5 checksum for a file
$ sudo md5sum file_name
Here is an example to run md5sum command
$ sudo md5sum data.txt bd456343c745c10323453a6565004e2f data.txt
It will display the 128-bit checksum along with file name. Please note a couple of important points of MD5 checksum generation.
- Checksum is generated based on file contents only, and not file name or path. So even if you rename/move the file, it will still result in the same checksum as before.
- If you change even 1 bit of the file’s contents, the resulting checksum will be different.
In fact, the above points are applicable for all checksums, no matter which algorithm is used to generate them.
So checksum calculation can be used to not only check if the received file is correct, but also to check if two files are duplicates of each other.
You may also use md5sum to generate checksum for a string, instead of a file. Here is an example where we pipe the output of echo command to md5sum to generate its checksum. You need to use -n option with echo command to remove trailing newline characters, if any, from the output.
$ echo -n "Hello World" | md5sum - dab7c402baab330a5464de1a230da21b -
Verify MD5 Checksum
In order to verify md5 checksums, you need to store them in a .md5 file. Here is an example to save checksum for file data.txt in checksum.md5
$ sudo md5sum data.txt > checksum.md5
It will store file path, along with its checksum. You can even store md5 checksums of multiple files in a single .md5 file
$ sudo md5sum data.txt data1.txt > checksum.md5
You can easily verify checksum with the following command
$ sudo md5sum -c checksum.md5
The above command will automatically open the files mentioned in .md5 file, calculate their checksum and compare them against the checksum values stored in it. Since, the .md5 file stores file paths, please do not move/rename your file after checksum file creation, otherwise the verification will fail.
That’s it. In this article, we have learnt how to calculate and verify MD5 hash of file in Linux. Checksum validation is a really useful to check if a large file received, such as ISO file, is correct or corrupted. It is also useful to quickly determine if two large files are duplicates of each other.
How to Install NVM on Mac using Brew
Shell Script to Start & Restart Tomcat Automatically in Linux
Shell Script to Backup MySQL Database
Htaccess Redirect If URL Contains String or Word
How to Change Root Password in MySQL