Home » 2014 » September » 13 » md5sum: calculate and check md5 message digest of a file in Linux

6:49 PM
md5sum: calculate and check md5 message digest of a file in Linux

       md5sum - compute and check MD5 message digest

       md5sum [OPTION] [FILE]...

       Print  or  check  MD5 (128-bit) checksums.  With no FILE, or when FILE is -, read standard input.

       -b, --binary
              read in binary mode

       -c, --check
              read MD5 sums from the FILEs and check them

       -t, --text
              read in text mode (default)

shanky@localhost:/home/shanky/test:> ll afile
-rw-rw-r-- 1 shanky group1 17 2014-09-13 14:42 afile
@localhost:/home/shankyk/test:> md5sum afile
816587a8471d79868c4ef63ee4a278ac  afile

In the above output you can see that the md5 sum of the file "afile" is 816587a8471d79868c4ef63ee4a278ac

Now if you change the file or modify the content of the file, the md5 checksum will change. So md5sum is the best command in linux to detect if a file has been modified or not.

[email protected]:/home/shanky/test:> cat>>afile

[email protected]:/home/shanky/test:> md5sum afile
e5c7f99016b0e2083693552e36f2ec09  afile

Here, you may notice that the checksum has chenged!!

Example: Suppose you want to detect that the file is modified and the moment it is modified, print a message "The file is modified"

md5cur=$(md5sum file|cut -d " " -f1);

        until [[ $md5new != $md5cur ]];    do
            md5new=$(md5sum afile|cut -d " " -f1);
                    if [[ $md5new != $md5cur ]]; then
                        print "The file is mofdified!";


In the above piece of script, we have stored the current md5 checksum of the file into "md5cur". Now we are using an until loop to compare the current md5 checksum with the new one continuously.

Until the new checksum is different from the current one, the loop will continue. And the moment someone makes any chanegs in the file, the loop condition fails and we get a message "The file is modified!"

Another use of this command is to make sure that the two files are same. The two files no maater where they are located, if they are having similar md5 checksum, they are totally identical files.


Category: Open System-Linux | Views: 1120 | Added by: shanky | Tags: calculate checksum of a file in lin, how to find if a file has been modi, md5 checkcum, shanky's portal, md5sum | Rating: 5.0/1

Related blogs

You may also like to see:

[2014-04-10][Open System-Linux]
SED command for filtering and transforming texts of a file using regular expression
[2014-01-08][Open System-Linux]
SSH Login Setup
[2015-05-25][Open System-Linux]
Password-less Login to Linux/Unix Servers in Putty
[2016-04-14][Open System-Linux]
AT command in Linux : schedule a task at later time
[2015-01-12][Open System-Linux]
W3M : A text based web browser and pager from Linux terminal

Total comments: 0