0

I have file names in the following format:

images.jpeg-028

I would like to transform the file name of every file in a directory from the format above to the following format:

images-028.jpeg

All the numbers at the end of the filenames are three digits long.

Based on this thread on another forum, I am thinking of something along the lines of:

for i in *; do mv "$i"\-(\d+) \-(\d+)"$i"; done

But am open to other Bash-based approaches.

HMLDude
  • 1,547
  • 7
  • 27
  • 47

1 Answers1

1

Having done countless file renaming tasks using for loops in bash, I now find the rename utility invaluable.

This should work for your case:

rename 's/\.jpeg-(\d+)/-$1.jpeg/g' images.jpeg-*

Note: I'm referring to the File::Rename module from Perl, not the rename utility that is included on many Linux distros in the util-linux package.

If you already have the version from util-linux, you may want to read this:

Get the Perl rename utility instead of the built-in rename.

If you're set on using a pure bash solution, or you just don't want the hassle of installing rename, this should work:

for i in images.jpeg-*; do
   mv "$i" "images-${i##*-}.jpeg"
done
Mike Holt
  • 4,452
  • 1
  • 17
  • 24