Rename files to md5 sum + extension (BASH)

ICTdesk.net picture ICTdesk.net · Nov 20, 2011 · Viewed 10.4k times · Source

I need some help with a bash script. Script needs to rename all files in a directory to its md5 sum + extension.

I have found the bash script below, but it needs to be changed so that it will add the extension.

md5sum * | sed 's/^\(\w*\)\s*\(.*\)/\2 \1/' | while read LINE; do mv $LINE; done

Answer

knittl picture knittl · Nov 20, 2011

I would go this route:

for F in $DIR/*.*; do
  mv "$F" "$(md5sum "$F" | cut -d' ' -f1).${F##*.}";
done

Use ${F#*.} to get everything after the first period, e.g. tar.gz instead of gz (depends on your requirements)