How to automate comparison of md5sum hash values for a large number of files
For example I have a file called test_binary
.
MD5 sum of file test is ef7ab26f9a3b2cbd35aa3e7e69aad86c
To test it automatically run this:
$ md5sum -c <<<"ef7ab26f9a3b2cbd35aa3e7e69aad86c *path/to/file/test_binary"
test_binary: OK
or
$ echo "595f44fec1e92a71d3e9e77456ba80d1 filetohashA.txt" | md5sum -c -
Quote from man
-c, --check
read MD5 sums from the FILEs and check them
Quote from wiki
Note: There must be two spaces between each md5sum value and filename to be compared. Otherwise, the following error will result: "no properly formatted MD5 checksum lines found".
Link to wiki
Also you can just read md5 hashes from file
$ md5sum -c md5sum_formatted_file.txt
It is expecting file with format:
<md5sum_checksum><space><space><file_name>
About *
and <space>
after MD5 sum hash. There is little note in man:
When checking, the
input should be a former output of this program. The default mode is
to print a line with checksum, a character indicating input mode ('*'
for binary, space for text), and name for each FILE.
And here is link to stackoverflow where I found answer on question, why should we, sometimes, distinguish binary
files and text
files.
One possibility is to use the utility cfv
sudo apt-get install cfv
CFV supports many types of hashes, and both testing and hash file creation.
# List the files
$ ls
test.c
# Create a hash file
$ cfv -tmd5 -C
temp.md5: 1 files, 1 OK. 0.001 seconds, 302.7K/s
# Test the hash file
$ cfv -tmd5 -T
temp.md5: 1 files, 1 OK. 0.001 seconds, 345.1K/s
# Display the hash file
$ cat *.md5
636564b0b10b153219d6e0dfa917d1e3 *test.c