Compare md5 of two files in shell script
What is MD5?
The MD5 Message-Digest Algorithm is a widely used cryptographic hash function that produces a 128-bit (16-byte) hash value. Specified in RFC 1321, MD5 has been employed in a wide variety of security applications, and is also commonly used to check data integrity. <<More>>
md5deep or md5sum commands are used to get the MD5 value for the files.
If you dont have these commands in your ubuntu, then type the below command and install the md5deep package.
1 2 3 4 5 6 7 8 9 10 11 12 13 | $ sudo apt-get install md5deep [sudo] password for kamaraj: Reading package lists... Done Building dependency tree Reading state information... Done ................ ...................... Selecting previously deselected package md5deep. (Reading database ... 251462 files and directories currently installed.) Unpacking md5deep (from .../md5deep_3.4-3_i386.deb) ... Processing triggers for man-db ... Setting up md5deep (3.4-3) ... $ |
Once installed, type the below command and check whether you have md5deep and md5sum commands in your system.
1 2 3 4 5 6 7 8 9 10 11 | $ md5sum --version md5sum (GNU coreutils) 7.4 Copyright (C) 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Ulrich Drepper, Scott Miller, and David Madore. $ md5deep -v 3.4 |
Now create some text files or take any existing file. Now, we will find out the MD5 value for that file.
I have a file called A.txt and now we will find out the MD5 value for that file.
1 2 3 4 5 6 7 8 9 10 11 12 13 | $ cat A.txt A B C D E F $ md5sum A.txt 6af4cbb084064df395d07ecac556de95 A.txt $ md5deep A.txt 6af4cbb084064df395d07ecac556de95 /home/kamaraj/Desktop/Scripts/A.txt |
How to compare the two MD5 values?
1 2 3 4 5 6 7 8 9 10 11 | $ a=$(md5deep -q A.txt) $ b=$(md5deep -q B.txt) $ [ "$a" = "$b" ] && echo "Equal" || echo "Not Equal" Equal $ b=$(md5deep -q C.txt) $ [ "$a" = "$b" ] && echo "Equal" || echo "Not Equal" Not Equal |
Recent Comments