How to rename multiple files to their contents’ MD5 sum

hashsumrenamescripting

I would like to rename some files to their contents' MD5 sum; for example, if file foo is empty, it should be renamed to d41d8cd98f00b204e9800998ecf8427e.

Does it have to be script or can I use something like the rename tool?

Best Answer

Glenn's answer is good; here's a refinement for multiple files:

md5sum file1 file2 file3 | # or *.txt, or whatever
    while read -r sum filename; do
        mv -v "$filename" "$sum"
    done

If you're generating files with find or similar, you can replace the md5sum invocation with something like find . <options> -print0 | xargs -0 md5sum (with the output also piped into the shell loop).

This is taking the output of md5sum, which consists of multiple lines with a sum and then the file it corresponds to, and piping it into a shell loop which reads each line and issues a mv command that renames the file from the original name to the sum. Any files with identical sums will be overwritten; however, barring unusual circumstances (like if you're playing around with md5 hash collisions), that will mean they had the same contents, so you don't lose any data anyway. If you need to introduce other operations on each file, you can put them in the loop, referring to the variables $filename and $sum, which contain the original filename and the MD5 sum respectively.

Related Question