Overview
The md5sum command generates an MD5 digest for files and can verify whether file contents have changed. It can also be used to determine if two files are identical. Because md5sum computes the checksum from file contents, it only verifies content and cannot detect changes to file metadata such as permissions or attributes.
Example: Creating Copies
[root@xuexi ~]# cp -a /etc/fstab /tmp/fstab
[root@xuexi ~]# cp -a /etc/fstab /tmp/fstab1
Generate MD5 Checksums
[root@xuexi ~]# md5sum /tmp/fstab /tmp/fstab1
a612cd5d162e4620b442b0ff3474bf98 /tmp/fstab
a612cd5d162e4620b442b0ff3474bf98 /tmp/fstab1
The identical MD5 values indicate the two files have the same content.
Saving Checksums and Verifying Later
Because each checksum line includes the path to the corresponding file, you can save the output to a file and later use that file to check whether the contents have been modified.
[root@xuexi ~]# md5sum /tmp/fstab /tmp/fstab1 > /tmp/fs.md5sum
[root@xuexi ~]# md5sum -c /tmp/fs.md5sum
/tmp/fstab: OK
/tmp/fstab1: OK
Modify /tmp/fstab1 and check again:
[root@xuexi tmp]# echo aaa >>/tmp/fstab1
[root@xuexi tmp]# md5sum -c /tmp/fs.md5sum
/tmp/fstab: OK
/tmp/fstab1: FAILED
md5sum: WARNING: 1 of 2 computed checksums did NOT match
Options When Using -c
- --quiet: Do not display records that verify as OK
- --status: Produce no output; use the command exit status to determine the result. If any record failed, the exit status is 1, otherwise 0.
[root@xuexi tmp]# md5sum --status -c /tmp/fs.md5sum
[root@xuexi tmp]# echo $?
1
Batch Comparison
By comparing md5sum values you can determine whether multiple files have identical contents. For larger sets of files, it is convenient to automate the comparisons with a script.