For When You Can't Have The Real Thing
[ start | index | login ]
start > Rename All Files To Be Their MD5Sum Values

Rename All Files To Be Their MD5Sum Values

Created by dave. Last edited by dave, 7 years and 107 days ago. Viewed 1,646 times. #3
[diff] [history] [edit] [rdf]
labels
attachments
(2016-12-31)

Problem

I have a ton of randomly named pictures. To make them consistently inconsistent, I want the on-disk filename to be the md5sum of the file content. (This acts as a paper-bag way of detecting gross duplicates. Doing less gross-duplicate-detection is left as an exercise for the reader's google skillz.)

Oh, and the file names have spaces in them.

Solution

This renames all files in the current directory to be the md5sum of their contents, preserving the file extension. There's probably a clever one-liner that does this but I'm tired and this works for me.

#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for i in *
do
  EXT=`echo $i | sed -e 's/^.*\./\./'`
  SUM=`md5sum $i |awk '{print $1}'`
  if [ -e "$SUM$EXT" ] ; then
    echo -n !
  else
    echo -n .
    mv $i $SUM$EXT
  fi
done
# restore $IFS
IFS=$SAVEIFS
no comments | post comment
This is a collection of techical information, much of it learned the hard way. Consider it a lab book or a /info directory. I doubt much of it will be of use to anyone else.

Useful:


snipsnap.org | Copyright 2000-2002 Matthias L. Jugel and Stephan J. Schmidt