0
Thanks

A few words of thanks would be greatly appreciated.

Shell/Linux - Rename files and add numbering

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.

0
Thanks

A few words of thanks would be greatly appreciated.

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jean-François Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.
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).