Shell/Linux - Rename files and add numbering

December 2016



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 :

This document entitled « Shell/Linux - Rename files and add numbering » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.