Batch renaming files in Sequential order with Bash(in windows)

Mar 4th, 2013 | By | Category: Windows

So let’s say you save 50 related files from the internets and they are all uselessly named like ksaljf23_13.jpg , etc etc and you want them to be sequentially named something relevant like videoGame_01.jpg videoGame_02.jpg oxycontin.  How can you go and batch rename them all with a quick and dirty script?

Well in linux it’s a simple matter, in windows however you need to bring a little linux in with you, or more specifically a little bash with CygWin.  I think I’ve blogged about cygwin before so go research that but you’re going to need an environment in which bash can run properly so go read up and install that.

Now onto the renaming script.  It’s incredibly simple and goes a little something like this:

a=1
for i in *.jpg; do
  new=$(printf "%04d.jpg" ${a}) #04 pad to length of 4
  mv ${i} ${new}
  let a=a+1
done

Just copy that into a file, chmod 755 and you are good to go.   alternatively I just changed “${new}" to "videoGame_${new}" as that was the topic of all of my images but for you obviously something different wuold be in order :)

 

Another even quicker and dirtier way to do it is  just through ls with this command:

ls *.JPG| awk 'BEGIN{ a=0 }{ printf "mv %s gopro_%04d.jpg\n", $0, a++ }' | bash

 

Tags: , , , ,

Leave a Comment