LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-02-2009, 05:08 AM   #1
shy_guest
LQ Newbie
 
Registered: Sep 2009
Location: In another land where the breeze and the trees and flowers glow blue
Distribution: Ubuntu 9.04, distrohopper
Posts: 11

Rep: Reputation: 0
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.
 
Old 09-02-2009, 05:19 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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?
 
Old 09-02-2009, 05:54 AM   #3
shy_guest
LQ Newbie
 
Registered: Sep 2009
Location: In another land where the breeze and the trees and flowers glow blue
Distribution: Ubuntu 9.04, distrohopper
Posts: 11

Original Poster
Rep: Reputation: 0
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
 
Old 09-02-2009, 05:57 AM   #4
shy_guest
LQ Newbie
 
Registered: Sep 2009
Location: In another land where the breeze and the trees and flowers glow blue
Distribution: Ubuntu 9.04, distrohopper
Posts: 11

Original Poster
Rep: Reputation: 0
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.
 
Old 09-02-2009, 07:24 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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:
Code:
#!/bin/bash
#
#  Define the starting search point:
#
start="/path/to/the/upper/level/dir"

#
#  Define the prefix for renaming
#
prefix="image_"

#
#  Initialize progressive number
#
count=100

#
#  Loop over JPEG images. Note: no chronological sorting is applied here.
#
while read file
do
  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.
 
Old 09-02-2009, 07:28 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by shy_guest View Post
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!
 
Old 09-02-2009, 07:58 AM   #7
shy_guest
LQ Newbie
 
Registered: Sep 2009
Location: In another land where the breeze and the trees and flowers glow blue
Distribution: Ubuntu 9.04, distrohopper
Posts: 11

Original Poster
Rep: Reputation: 0
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.
 
Old 09-02-2009, 01:40 PM   #8
emgee_1
Member
 
Registered: Aug 2009
Location: Nijmegen, The Netherlands
Distribution: Slackware 14
Posts: 42

Rep: Reputation: 6
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

Greetings

Marcel from the Netherlands
 
  


Reply

Tags
bash, photos, scripting



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to rename photos in directory & all subdirectories shy_guest Linux - Software 1 09-02-2009 05:28 AM
Using Bash, Find script files in a directory or subdirectories within... ray5_83 Programming 4 10-10-2008 07:42 PM
copy and rename the existing directory using BASH script neo2k Linux - Software 3 08-20-2008 10:43 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM
Bash Expression: Current Directory & All Subdirectories Below gmcauley Programming 2 01-01-2007 02:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration