-   Linux - Software (
-   -   Bash script to rename photos in directory & all subdirectories (

shy_guest 09-02-2009 06:08 AM

Bash script to rename photos in directory & all subdirectories
I have several thousand photofiles both scanned & from various digital cameras. I have organised them more or less chronologically into directories by year and subdirectories by month. They are all in JPEG format but file extensions vary from .jpg to .JPG & perhaps even .JPEG

The scanning software has given me over hundreds of files with the same name, since after the standard prefix, it numbers them uniquely only within the current directory. Various cameras have given their files different prefixes before a file number.

I would like to rename them all uniquely by giving each one a prefix followed by a padded 7 figure chronological serial number starting at 0000100 (in fact numbering them by hundreds so that if I find any further photos, I can insert them in the gaps without too much hassle) followed by the extension .jpg. I want the script to be easily modifiable so that all subsequent batches can be given the same treatment.

I want to keep the files in their current directories & subdirectories.

I have looked but don't see any software that seems to do this & it occurred to me that a bash script should be able to do it, but I don't know enough to write the script.

Can a bash script expert please help me.

colucix 09-02-2009 06:19 AM

It should be not so difficult, but you have to establish exact criteria to sort them and apply numbering. You stated: organized them more or less chronologically. So do you want to number according to the chronological order? The "more or less" part worries me. How can you determine the exact date of the click?

Regarding original digitized photos, you can easily extract this information using (the most likely already installed) imagemagick, but only if the camera did not erase this information. Regarding scanned images, you can retrieve only the last modification time of the image file itself, but you can't tell when the original click has done.

So please, clarify these criteria before proceeding. Also, what is your shell scripting knowledge? Have you already written some code to show us for debugging/improving?

shy_guest 09-02-2009 06:54 AM

colucix - not that worried about absolutely precise chronological order of stuff that I scanned. I have scanned photos from 1950s to 2002 coming from packets of photos that were dated or loose photos that I can date by context (primary school class photos from specific class in 50s, etc) to which I am adding stuff from family albums going back to 1890s where it is a question of guessing the right decade, where nothing is written on the back.

My knowledge of bash scripting is pretty rudimentary - I have read a few scripts & know that I will have to use find to explore the subdirectories, mv to rename the files & some local variable or perhaps argument to specify the starting number. probably some braces to specify the files I am searching for in the find statement, a %7 to pad the numbers with leading zeroes, etc. What is much less clear is whether the mv statement will remember which subdirectory the file is in, or whether I need a local variable. I don't yet have a

shy_guest 09-02-2009 06:57 AM

oops ! preliminary version of a script, as doing the scanning takes up a sizable chunk of my spare time.

Sorry about the double post. That occurred because after I tried to post, I was asked to login (I was already logged in having just registered for the forum) & after logging in was returned to the post with no indication that it had already been posted.

colucix 09-02-2009 08:24 AM

Ok. Here is a basic script that can serve as a starting point. You're correct in your guess about the required shell statements: you miss just the step to join them together:

#  Define the starting search point:


#  Define the prefix for renaming


#  Initialize progressive number


#  Loop over JPEG images. Note: no chronological sorting is applied here.

while read file
  dir=$(dirname "$file")
  echo mv "$file" "$dir"/$prefix$(printf "%07d" $count).jpg
  count=$((count + 100))
done < <(find $start \( -iname \*.jpg -o -iname \*.jpeg \))

The block of code inside the loop 1. extracts the file path, 2. rename the file according to your requirement, 3. update the progressive number. The input to the loop comes from a "process substitution" that uses the find command to locate all the jpg and jpeg images (in a case-insensitive way).

Note that I put a leading echo in the renaming statement. This is for testing purposes: the mv commands are just displayed in the terminal and not actually executed. In this way you can review the result of the script without blaming you if something goes wrong. Once you've carefully reviewed the displayed mv commands, you can remove the echo statement and run again to apply changes.

Now is up to you to find a way to sort the images in chronological order, that is you have to develop an additional block of code which will come before the mv statement or perhaps inside the "process substitution" itself.

colucix 09-02-2009 08:28 AM


Originally Posted by shy_guest (Post 3666634)
Sorry about the double post. That occurred because after I tried to post, I was asked to login (I was already logged in having just registered for the forum) & after logging in was returned to the post with no indication that it had already been posted.

Don't worry. I hope I was not rude in my advice. I just wanted to be sure the upcoming discussion were placed in a single thread! :)

shy_guest 09-02-2009 08:58 AM

Thanks a million for the script.

The fact that you can put the input after the done of the do-loop amazes & delights me - the very first thing you do comes right at the end of the script !

Now back to the scanning.

emgee_1 09-02-2009 02:40 PM

Hi there, I would also suggest some open source sofware called : exifsorter; (google for it)
Here you can, based on the exif data, sort and rename all your photo's. At the same time you can, if you wish so, put them in folders that are for instance named by date such as /photodir/2009/08/Italy and in that dir: 2009-08-11 14:12:13 Rome.jpg
Which makes it also easy for sorting; there is also rename capability in for instance thunar (xfce desktop), pyrename (ubuntu repos) or krename (kde 3.5 or possibly 4.x.x) take your pick


Marcel from the Netherlands

All times are GMT -5. The time now is 12:49 PM.