Someone asked How to ensure that two files are different (if date and size is same)? on Arch Linux Forums. I immediately thought about cmp command which I have recently stumbled across via cksum on Wikipedia, cmp is listed after cksum.

1   cmp

cmp - compare two files byte by byte

I knew it’s easy to tell with cmp command, but then I recalled I was still using diff embarrassingly and ineffectively. Don’t get me wrong, diff can get the job done, for example:

if ! diff fileA fileB >/dev/null; then
  : files are different, do something
fi

It can run as expected, but ineffective, because it would list the diff. We only care if the files are the same or not. By using cmp:

if ! cmp --silent fileA fileB; then
  : files are different, do something
fi

This is better and we don’t even need to mute the output when there is a --silent option.

2   Checksums and hashes

If the files are big and some may be checked multiple time against others, it may be better to use checksum or hashes. Pick one you like:

echo {,ck,md5,sha{,1,224,256,384,512}}sum
eval {,ck,md5,sha{,1,224,256,384,512}}sum' /dev/null;'
sum cksum md5sum shasum sha1sum sha224sum sha256sum sha384sum sha512sum
00000     0
4294967295 0 /dev/null
d41d8cd98f00b204e9800998ecf8427e  /dev/null
da39a3ee5e6b4b0d3255bfef95601890afd80709  /dev/null
da39a3ee5e6b4b0d3255bfef95601890afd80709  /dev/null
d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f  /dev/null
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855  /dev/null
38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b  /dev/null
cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63