Does the MD5SUM for a file change when I make a copy of the file?


$ md5sum file01.dmp
$ cp file01.dmp file02.dmp
$ md5sum file02.dmp

Shouldn't the two MD5SUMs match?

  • hd5 sum must match, can you give more details ? note also that filename part will of course not match. – Archemar Mar 4 '16 at 16:16
  • 3
    if cp was successful, the answer is 'yes'. Is that your actual question, though? Did something go wrong, or is this a theoretical question? – Jeff Schaller Mar 4 '16 at 16:21
  • @Jeff - the cp did not report errors. I had a directory with database dump files. I made a copy of the entire directory and when I compared the MD5SUMs - I got different values for same filename (in different directory). Thinking somehow the directory could be a factor, I then made copy of one of file in same directory and again got different MD5SUM. That was the reason behind my question. – oradbanj Mar 4 '16 at 16:42

md5sum (and sha1sum and sha256sum etc.) compute a hash of the file's contents. This does not include the filename or any other metadata (like modification times). If two files have the same contents, then md5sum will generate the same hash for each. (Note that the output of md5sum consists of the hash and the filename. The hash will not change if you rename the file, but of course the filename portion of the output will change.)

It is always true that two files that generate different hashes have different contents. If a copied file generates a different hash, then the copy failed in some way, or one of the files was modified after the copy.

However, it is not necessarily true that two files that generate the same hash have the same contents. Since hashes are a fixed size, there are a large number of different files that will all generate the same hash. That is called a collision. But finding a collision is not supposed to be easy. (It is for MD5, though, which is why it's no longer considered secure. MD5 is still plenty good enough for detecting accidental file corruption, just not malicious modification.)

| improve this answer | |
  • The hash will be the same, but the output includes the filename, so the output will change. – Otheus Mar 4 '16 at 16:47
  • @Otheus, I'm assuming the OP is intelligent enough to figure that out. – cjm Mar 4 '16 at 16:51
  • @Otheus - when I say MD5SUM, I mean the hash only. Yes - I know that the filename will be different. – oradbanj Mar 4 '16 at 16:59
  • 2
    Then it's likely that your .dmp was still being written to after the copy. – Otheus Mar 4 '16 at 17:00

The md5sum output will change because it reports on the filename itself in the last field. To strip the filename so that only the hash itself is output, use awk or cut:

md5sum filename | cut -d ' ' -f1
| improve this answer | |
  • 1
    md5sum <filename – roaima Mar 4 '16 at 17:07
  • @Roaima Sure enough, but the cut- form is more general, ie, for use with find or multiple input files. – Otheus Mar 4 '16 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.