md5sum Command in Linux with Examples
The md5sum is designed to verify data integrity using
MD5(Message Digest Algorithm 5). MD5 is 128-bit cryptographic hash and if used properly it can be used to verify file authenticity and integrity. Example :
Input : md5sum /home/mandeep/test/test.cpp
Output : c6779ec2960296ed9a04f08d67f64422 /home/mandeep/test/test.cpp
Importance :
Suppose, anyone wants to install an operating system , so to verify if it's correct CD, it's always a good idea to verify .iso file using MD5 checksum, so that you don't end up installing wrong software (some sort of virus which can corrupt your filesystem).
Syntax :
md5sum [OPTION]... [FILE]...
It will print or check MD5(128-bit) checksum. It computes MD5 checksum for file "test.cpp" Output :
c6779ec2960296ed9a04f08d67f64422 /home/mandeep/test/test.cpp
Options :
-b :
read in binary mode
-c :
read MD5 from files and check them
--tag :
create a BSD-style checksum
-t :
read in text mode(it's by default)
The options which are useful when verifying checksum :
--ignore-missing :
don't report status for missing files
--quiet :
don't print OK for each successfully verified file
--status :
don't output anything, status code shows success
--strict :
exit non-zero for improperly formatted checksum files
-w :
warn about improperly formatted checksum files
Command usage examples with options :
Example 1: Store the MD5 checksum in file and then verify it.
# md5sum /home/mandeep/test/test.cpp > checkmd5.md5
It will store the MD5 checksum for test.cpp in file checkmd5.md5
# md5sum -c checkmd5.md5
It will verify the contents of file Output :
/home/mandeep/test/test.cpp: OK
After changing the contents of file checkmd5.md5, the output will be :
/home/mandeep/test/test.cpp: FAILED
md5sum: WARNING: 1 computed checksum did NOT match
Example 2: create a BSD-style checksum with --tag option
# md5sum --tag /home/mandeep/test/test.cpp
Output :
MD5 (/home/mandeep/test/test.cpp) = c6779ec2960296ed9a04f08d67f64422
Example 3: --quiet option, can be used when verifying checksum, don't print OK when verification is successful.
# md5sum -c --quiet checkmd5.md5
Don't produce any output, means it's successful. But if checksum don't match, it produces warning.
# md5sum -c --quiet checkmd5.md5
/home/mandeep/test/test.cpp: FAILED
md5sum: WARNING: 1 computed checksum did NOT match
Example 4: --warn option, it can be used for generating a warning for improperly formatted checksum files. content of file checkmd5.md5:
c6779ec2960296ed9a04f08d67f64422 /home/mandeep/test/test.cpp
Now, execute command with --warn option
# md5sum -c --warn checkmd5.md5
/home/mandeep/test/test.cpp: OK
It don't produce any warning. Now, do some formatting in file checkmd5.md5
c6779ec2960296ed9a04f08d67f64422
/home/mandeep/test/test.cpp
Now, execute the command
# md5sum -c --warn checkmd5.md5
Output :
md5sum: checkmd5.md5: 1: improperly formatted MD5 checksum line
md5sum: checkmd5.md5: 2: improperly formatted MD5 checksum line
md5sum: checkmd5.md5: no properly formatted MD5 checksum lines found
and if --warn is replaced with --strict option, it will exit non-zero for improperly formatted checksum lines
# md5sum -c --strict checkmd5.md5
md5sum: checkmd5.md5: no properly formatted MD5 checksum lines found