Create a gist now

Instantly share code, notes, and snippets.

@cympfh /rename.sh
Last active Aug 29, 2015

What would you like to do?
rename a file to its md5 (with its extension)
#!/bin/bash
if [ $# != 1 ]; then
echo 'usage: % <file>'
exit 0
fi
MD5=md5
which md5sum > /dev/null && MD5=md5sum
TARGET=$1
EXT=${TARGET##*.}
which md5sum > /dev/null
if [ $? = 0 ]; then
HASH=`md5sum $TARGET | cut -d ' ' -f1`
else
HASH=`md5 $TARGET |awk '{print $NF}'`
fi
if [ $EXT = $TARGET ]; then # no extension?
echo mv $TARGET ${HASH}
mv $TARGET ${HASH}
else
echo mv $TARGET ${HASH}.${EXT}
mv $TARGET ${HASH}.${EXT}
fi
Owner
cympfh commented Jul 2, 2015
% wget -O ~/bin/ren https://gist.githubusercontent.com/cympfh/e3a861bbbb0d0002e6a7/raw/d6bdd35250ecb0490a8b4e4a4555495f8f47a54f/rename.sh && chmod +x ~/bin/ren
% ren CIExbBuUcAEsTIW.jpg:large.jpeg
mv CIExbBuUcAEsTIW.jpg:large.jpeg 0d64e244497219e306f2d6dacbde1bf6.jpeg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment