(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