Rename files based on checksum

checksumfileshashsumrename

I have a md5sum list and a lot of file which I wanted to checksum and then rename them according to the md5sum list.

Example of the list:

d4cd401ade018617629b39efed7b7be4  foo.bar
8fdb07ca55c164e0d5a69eff49fe800e  bar.foo
8b167d01009f066aaf2d6c1ba336d842  foobar

Now I wanted to checksum every files in current directory, if the checksum are matched with the list above then rename it as the right colum.

How I can do that?

Best Answer

I haven't fully tested, it's just theoretically working. Substitute where needed:

#! /bin/bash
for II in *
do
    if [ -f "$II" ]; then
        TMPV=$(md5sum "$II")
        MD="${TMPV%\ \ *}"
        TMPV=$(grep "$MD" hashes.txt)
        if [ ! -z "$TMPV" ]; then
            FN="${TMPV#*\ \ }"
            echo "Found: $II"
            echo "MD5 is: $MD"
            echo "Which matches $FN in hashes database"
            echo "Will Rename $II TO $FN"
            echo ""
            # CAREFUL, RENAME CMD: mv "$II" "$FN"
        fi;
    fi;
done;

As I say, haven't tested it, but it seemed to work on my box.

Related Question