Bash: Bulk Renaming
I had to rename several hundred thousand files today. Thanks to a botched invocation of ImageMagick, they all looked like unique_prefix.png.jpg, whereas we simply wanted unique_prefix.jpg.
I found a suitable answer at the Unix StackExchange. As one of the many variants of parameter substitution, Bash supports ${var/Pattern/Replacement}: “first match of Pattern within var replaced with Replacement.”
for f in *.png.jpg; do mv $f "${f/.png}" done
The target expression could also have been written as "${f/.png.jpg/.jpg}"
blog comments powered by Disqus