Shell/Linux - Rename files and add numbering

January 2017



Issue


Well, I'd like to create a sh that allows me to rename a group of jpg files as such.
IMGP2345.jpg | IMGP2346.jpg | IMGP2347.jpg

to
IMG_0001.jpg | IMG_0002.jpg | IMG_0003.jpg 

Solution


Try this:

#! /bin/bash

i=1
j=0000

for fic in IMG*
do
   k=${j}${i}
   #mv -v "${fic}" "${fic/P????/_${k: -4}}"
   echo "mv -v ${fic} ==>> ${fic/P????/_${k: -4}}"
   ((i++))
done


If the result displayed fits to your needs, uncomment
#mv -v "${fic}" "${fic/P????/_${k: -4}}"
And comment the one beginning by "echo".


Thanks to zipe31 for this tip.

Related


Published by aakai1056. Latest update on October 20, 2011 at 08:42 AM by aakai1056.
This document, titled "Shell/Linux - Rename files and add numbering," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).