#!/bin/bash

for filename in $(ls | grep -E "(^|_)[[:digit:]]{13}\.[[:alpha:]]{3,4}$")
do
	filehash=$(md5sum "$filename" | sed -E "s/ \*.*$//")
	newfilename=$(echo $filename | sed -E "s/[[:digit:]]{13}/$filehash/")
	if mv -n $filename $newfilename
	then
		echo "$filename -> $newfilename"
	fi
done

Responses

In response to blip #122364

Oberschutze said:

#!/bin/bash

for filename in $(ls | grep -E "(^|_)[[:digit:]]{13}\.[[:alpha:]]{3,4}$")
do
	filehash=$(md5sum "$filename" | sed -E "s/ \*.*$//")
cut -d \ -f1

is more succinct than this sed incantation.

newfilename=$(echo $filename | sed -E "s/[[:digit:]]{13}/$filehash/")

What's the meaning of the leading _ which you are (possibly) preserving here?

if mv -n $filename $newfilename
	then
		echo "$filename -> $newfilename"
	fi
done

Could alternatively write this as:

mv -nv $filename $newfilename
done | sed 's/^renamed //'

if it's what I think (you just want a shorter version of the report that mv gives).

  • 1