Saturday 20 April 2013

Renaming large number of files

This post is just for those who are new to shell scripting. Today I stuck with a problem of renaming a large number of a pdf file. I was trying to rename them like "sequence-point-stack-1" only the last number will change and rest will be same for all files. So I tried to write a script that will help me and ultimately I succeed. Here is the code:



 #!/bin/bash
 j=1
 for i in *.pdf
 do
 mv $i "sequence-point-stack-$j.pdf"
 j=$((j+1))
 done
In this script you just need to change the extension as your requirement and the name that you want to give the files. But beware of this fact that if extension is not part of the name of the file this script wont do. Like if you a have two pdf  file "lolo" and "lola.pdf" then only the "lola.pdf" will be affected. Another thing that it wont work for those case where you want to rename the file with totally different strings. For example if you want to rename two file as "hehe-1" and "hoho-2" then this script wont work. In generalized case the script will look like:

#!/bin/bash
 j=1
 for i in *.<<your extension>>
 do
 mv $i "<<the name you want to give>>$j.<<your extension>>"
 j=$((j+1))
 done

** Don't forget to remove "<<" and ">>".

No comments:

Post a Comment