1

I have a bunch of images that I'd like to rename as follows:

  • *.png.png --> *.png
  • *.jpeg.jpg --> *.jpg
  • *.JPEG --> *.jpg

The only thing I've tried thus far is mv *.png.png *.png, but I knew that wouldn't work, but took a chance nevertheless.

Is there a simple (or maybe not) way to batch rename files with this pattern?

1
  • If you are seeking a command try mmv. It allows to specify wildcards; make sure you use quotes so that the shell does not expand those wildcards and mmv can act on those.
    – Janis
    Apr 8, 2015 at 6:59

3 Answers 3

3

Here is a shell-only solution for the three cases you have in your question:

for f in *.png.png ; do mv -i "${f}" "${f%.png}" ; done

for f in *.jpeg.jpg ; do mv -i "${f}" "${f%.jpeg.jpg}.jpg" ; done

for f in *.JPEG ; do mv -i "${f}" "${f%.JPEG}.jpg" ; done

Type it in the directory where the files are to be renamed.

If you like to generalize this type of solutions in form of a replace suffix script you can do:

#!/bin/sh
from=${1:?} to=${2:?}
for f in *${from} ; do mv -i "${f}" "${f%${from}}${to}" ; done

and call that script with two parameters, the suffix to replace, and the new suffix.

2

Here is something using find to rename *.png.png -> *.png:

find ./ -name '*.png.png' -type f \
        -exec sh -c 'mv {} ./$(basename -s .png.png {}).png' \;

It isn't really gerenic, so you have to customize it for the other file extensions.

3
  • Thanks! Let me give that a bash (no pun intended) and let you know
    – maGz
    Apr 8, 2015 at 6:45
  • Works like a charm! Changed it for other extensions. Thank you, FloHimself
    – maGz
    Apr 8, 2015 at 7:05
  • 1
    You could use parameter expansion thus avoiding using command substitution. This will strip the extra file extension off the file name and rename the file -exec bash -c 'file={}; mv "$file" "${file%.*}"' \;
    – fd0
    Apr 8, 2015 at 14:56
0

Use rename. Specially designed to work with multiple files.

Shorter. Cleaner.

*.png.png --> *.png

% rename 's/.png.png$/.png/' *.png

*.jpeg.jpg --> *.jpg

% rename 's/.jpeg.jpg$/.png/' *.jpeg.jpg

*.JPEG --> *.jpg

% rename 's/.JPEG$/.jpg/' *.JPEG

More comprehensive manual :

% man rename

1
  • Notice that if this is following the sed regexp syntax, you should escape the . as otherwise it would also match imagepng.png for example (with the first . matching the e as "any single character)
    – AdminBee
    Jun 30, 2020 at 10:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .