14

I am confused how md5sum --check is supposed to work:

$ man md5sum
-c, --check
    read MD5 sums from the FILEs and check them

I have a file, I can pipe it to md5sum:

$ cat file | md5sum
44693b9ef883e231cd9f90f737acd58f  -

When I want to check the integrity of the file tomorrow, how can I check if the md5sum is still 44693b9ef883e231cd9f90f737acd58f?

Note

cat file might be a stream. So I want to use the pipe as in my example, not md5sum file.

5 Answers 5

25

You do this:

cat file | md5sum > sumfile

And the next day you can do this:

cat file | md5sum --check sumfile

Which prints:

-: OK

if everything is alright.

3
  • It should be noted that 'md5sum --check sumfile' does not read from stdin but reads from the files specified in the file 'sumfile'. This means the 'cat file |' part of the command 'cat file | md5sum --check sumfile' does nothing useful. In this specific case the result will be the same but if you change 'file' to another filename this will be ignored and this command may print '-: OK' even when the files differ.
    – OFE
    Jul 6, 2021 at 12:37
  • 1
    @OFE I have to disagree, please test it yourself in the shell... cat file | is needed in order to work. If cat is omitted md5sum will wait for input on stdin. Also if the file changed md5sum will print -: FAILED.
    – m13r
    Jul 7, 2021 at 12:48
  • 1
    You are correct, it would seem that the behaviour of md5sum depends on how sumfile was created. It seems my sumfile was created with something like md5sum file > sumfile. This means the sumfile contains the filename which is what causes md5sum --check sumfile to ignore the stdin and read from the file specified in sumfile. cat file | md5sum > sumfile causes the sumfile to contain a - character in place of the filename which is what causes md5sum --check sumfile to read from stdin. My apologies for not doing my homework before commenting.
    – OFE
    Jul 8, 2021 at 13:47
11

I assume that you do know the md5sum of the file.

Just issue the following command:

echo ff19e3f8bde936457b8e53c825110987 myfile | md5sum --check -
myfile: OK

md5sum --check, like md5sum with any other option (or none) takes input from stdin if no file (or -) is specified on the command line.

1

The default syntax for md5sum is:

$ md5sum file 
068a9a19124df814e52ff5461598cfe4  file

To create a checksum file, redirect standard output to a file:

$ md5sum file > md5.checksum

To verify the file against the checksum file:

$ cd path/to/file
$ md5sum --check path/to/md5.checksum
file: OK

That said, m13r's implementations is equally valid.

0

Bringing up a very old question here, but I had the same question. In my case, I was wanting to verify MD5Sums on some *.gz files I retrieved (via SFTP) over a relatively slow internet link.

The key to this is that MD5 sum will read standard in if no file is specified, or if '-' is given for the filename. Without the "--check" option, it will give an MD5SUM hash of the text from STDIN. With the "--check" option, but with no file specified, it will read the checksum "file" from STDIN.

So, for example, in my case, I was able to do this:

ssh user@remotehost 'md5sum *.gz'|md5sum --check

This ran the md5sum hash on the remote host, generating a checksum file for all the *.gz files, and then piped that into md5sum on my end, checking each file against the results from the remote side. I then saw the {filename}:OK for each file that I had transferred from the remote host (and if the file were incomplete or corrupt, I would've seen the errors).

0

for one liner to check from stdin:

cat file | md5sum --check <(echo "44693b9ef883e231cd9f90f737acd58f  -")

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .