MV

Thursday, August 12, 2010

Renaming files

When I travel, I always have at least two CF cards to take some, well most of my family say A LOT (but who cares...), pictures. My problem is always the numbering of the files, because I have set my camera to restart the numbering for a new card. Why, I don't have a clue, but it's like that... perhaps I should change my setting, but then you wouldn't be reading this blog message.

So my issue is that I want to put all files in one directory but then I have conflicting files because there will be 2 files named bla00001.jpg. So I have to add a fixed number to each file name of the second, third,... CF card in order to avoid these conflicts.

Perhaps my solution isn't the shortest, nor the best, but at least it works for me. The solution described here is originally created by my ex-colleague Erik.

So my camera produces the following file name format: DSC000001.JPG
Let's say that the highest number on my first CF card is 313, the first file name of the second CF card must then be 314, i.e. DSC00314.JPG.

So create a file somewhere on your hard drive, for example in /home/myworkspace/Pictures/rename.sh

Add this code to your file:

#!/bin/bash

for file in `ls *.jpg`
do
    numberpart=`echo ${file} | sed 's/DSC[0]*//g'`
    let number=`echo ${numberpart} | sed 's/\.jpg//g'`
    let number=number+313
    filename=DSC00${number}.jpg
    mv ${file} ${filename}
done


Save the file and make it executable via chmod +x rename.sh

Before testing it, make a backup of your pictures directory of which you need to rename the files!!!

Put the script in the directory and run it (./rename.sh)

Now I can put all my pictures in one dir, /me happy