Linux: Compare md5sum checksum

Creating md5sum checksum (or sha256sum) can check integrity of copied file. Because the scp command does not check file integrity, you need to check file integrity with md5sum command manually (rsync will check file integrity). This article will describe comparing md5sum checksum.

1 Compare md5sum checksum of file

Write md5sum checksum to md5 file.

$ md5sum file.txt | tee file.md5
9af2f8218b150c351ad802c6f3d66abe  file.txt

And then copy file.txt and file.md5 to remote server.

$ scp file.txt file.md5 <server>:<path>

In remote server, run "md5sum -c". If checksum is matched, "md5sum -c" will return 0.

$ md5sum -c file.md5
file.txt: OK
$ echo $?
0

If checksum is not matched or file is not exists, "md5sum -c" will return non 0 value.

$ md5sum -c file.md5
file.txt: FAILED
md5sum: WARNING: 1 computed checksum did NOT match
$ echo $?
1

2 Compare md5sum checksum of all files in directory

This article uses the following directory.

$ tree dir
dir
├── file1.txt
└── subdir
    └── file2.txt

You need to create md5sum checksum of all files in directory. The find command can omit file names from md5sum command.

$ cd dir
$ md5sum $(find . -type f) | tee ../dir.md5
3d709e89c8ce201e3c928eb917989aef  ./subdir/file2.txt
5149d403009a139c7e085405ef762e1a  ./file1.txt

And then copy dir and dir.md5 to remote server.

$ scp -r dir dir.md5 <server>:<path>

In remote server, run "md5sum -c". If all checksums are matched, "md5sum -c" will return 0.

$ cd dir
$ md5sum -c ../dir.md5
./subdir/file2.txt: OK
./file1.txt: OK
$ echo $?
0

If checksum is not matched or file is not exists, "md5sum -c" will return non 0 value.

$ cd dir
$ md5sum -c ../dir.md5
./subdir/file2.txt: FAILED
./file1.txt: OK
md5sum: WARNING: 1 computed checksum did NOT match
$ echo $?
1