bashubuntumd5windows-subsystem-for-linuxmd5sum

Comparing content of 2 files with md5sum


How can I compare the md5 sums for 2 files in one command?

I can compute them each individually:

my_prompt$ md5sum file_1.sql
20f750ff1aa835965ec93bf36fd8cf22  file_1.sql

my_prompt$ md5sum file_2.sql
733d53913c366ee87b6ce677971be17e  file_2.sql

But wonder how this can be combined into a single comparison computation. I have tried different approaches that fails:

my_prompt$ md5sum file_1.sql == md5sum file_2.sql
my_prompt$ `md5sum file_1.sql` == `md5sum file_2.sql`
my_prompt$ (md5sum file_1.sql) == (md5sum file_2.sql)
my_prompt$ `md5sum file_1.sql` -eq `md5sum file_2.sql`

What am I missing here ? Tried following Compare md5 sums in bash script and https://unix.stackexchange.com/questions/78338/a-simpler-way-of-comparing-md5-checksum without luck.


Solution

  • You need a program/built-in that evaluates the comparison. Usually you would use test/[/[[ to do so. With these programs -eq compares decimal numbers. Therefore use the string comparison = instead.

    [[ "$(md5sum file_1.sql)" = "$(md5sum file_2.sql)" ]]
    

    The exit code $? of this command tells you wether the two strings were equal.

    However, you may want to use cmp instead. This program compares the files directly, should be faster because it doesn't have to compute anything, and is also safer as it cannot give false positives like a hash comparison can do.

    cmp file_1.sql file_2.sql