How to verify the integrity of a file with md5 checksum

Checksum programs are used to generate checksum key strings from the files and verify the integrity of the files later by using that checksum string. A file might be distributed over the network or any storage media to different destinations. Due to many reasons, there are chances of the file being corrupted due to a few bits missing during the data transfer for different reasons. These errors happen most often while downloading the files from the Internet, transferring through a network, CD-ROM damage, and so on.

Hence, we need to know whether the received file is the correct one or not by applying some kind of test. The special key string that is used for this file integrity test is known as a checksum. The most famous and widely used checksum technique is md5sum.

Computing md5sum

1. We need to first compute the md5sum of the file. To print the md5 checksum on the terminal, use the md5sum command followed with the filename.

# md5sum file 
dcf21e3a1a0181294bceae07ec16a9d9  file

md5sum is a 32-character hexadecimal string as given.

2. You can also redirect the md5sum to a file as shown below.

# md5sum file > hash.md5

3. You can also create a md5sum hash for multiple files using the same command.

# md5sum file1 file2 file3

Verifying integrity of a file with md5 checksum

1. To verify that the alpah-numeric string that was printed to the terminal matches the md5 hash that was provided with the file. To verify md5 checksum from a file.

# md5sum -c hash.md5
file: OK

The program will print to the console the filename and ok if verified. The format of the file should be: hash(two spaces)filename.

# vim hash.md5
dcf21e3a1a0181294bceae07ec16a9d9  file

2. If the checksum of file fails, you would get an output as shown below.

$ md5sum -c hash.md5
file: FAILED

How to check checksum of all files in some directory recursively

Suppose you want to check checksum all files/subdirectory in some directory. md5sum utility doesn’t have the recursive option. Don’t worry, there is a small trick you can use here.

1. You can create md5sum list by combination of find and md5sum. For example:

# find [DirectoryName] -type f -exec md5sum {} \; > ~/md5sum.txt

above command allows you to create md5sum list for all files in some directory.

2. To check it, you can simply do as:

# md5sum --check md5sum.txt > result.txt

md5deep for calculating checksum on directories

Checksums are usually calculated on the files. But if you want to calculate them on a directory, you will have to calculate the checksums for all the files in the directory, recursively. This can be easily achieved by the “md5deep” utility. For example to calculate the md5sum on a directory.

# md5deep -rl /path/to/directory > directory.md5

Here,
-r – provides the recursive md5sum feature
-l – is used for the relative path. By default it writes absolute file path in output.

Related Post