LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-30-2009, 10:55 AM   #1
SuperJediWombat!
Member
 
Registered: Apr 2009
Location: Perth, Australia
Distribution: Ubuntu/CentOS
Posts: 208

Rep: Reputation: 51
Sorting photos into folders based on exif date


I am trying to create a bash script to take photos from a directory, rename and sort them based on the date found in the exif data made by my camera.

When the script is finished it should move all the files into this structure: ./YYYY-MM/YYMMDD-HHMMSS.ext

The section I am having trouble with is sorting the renamed files into folders.

This command
Code:
jhead -autorot -ft -nf%y%m%d-%H%M%S *
renames the files in the current folder to YYMMDD-HHMMSS.jpg taken from exif data.

I am new to scripting as I'm sure you can see, is there an easy way to strip the first four numbers from the file names, and sort them based on that?

Code:
#!/bin/sh
#

# move files from camera to sorting area
mv /<pathtomemcard>/* /<pathtosortlocation>/

# rename all image files in sorting area based on exif dates
# YYMMDD-HHMMSS.ext 
jhead  -autorot -ft -nf%y%m%d-%H%M%S /<pathtosortlocation>/*

# Sort files into folders based on the file name or exif date (whichever is easiest)
# NEED TO DO

# move sorted files into photo library
mv /<pathtosortlocation>/* /<pathtophotolib>/
Thanks in advance for any help!

Last edited by SuperJediWombat!; 04-30-2009 at 06:40 PM.
 
Old 04-30-2009, 01:21 PM   #2
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

you're using bash?

Untested:
Code:
ls <pathtosortlocation> | while read f; do
  # extract first 4 characters from filename
  YYMM=${f:0:4}
  # create directory if it does not exist
  test -d $YYMM || mkdir $YYMM
  # move the current file to the destination dir
  mv <pathtosortlocation>/$f $YYMM/$f
done
Jan
 
Old 04-30-2009, 07:30 PM   #3
SuperJediWombat!
Member
 
Registered: Apr 2009
Location: Perth, Australia
Distribution: Ubuntu/CentOS
Posts: 208

Original Poster
Rep: Reputation: 51
Thank you!

Thanks jan61, that worked perfectly.

Here is my script so far:
Code:
#!/bin/bash
#
#
CameraPath="/home/angus/st/camera"
SortLocation="/home/angus/st/sort"
PhotoLibrary="/home/angus/st/library"
CharFromName=4

############
# move files from camera to $SortLocation
mv $CameraPath/* $SortLocation/

############
# rename all image files in $SortLocation
# YYMMDD-HHMMSS.ext
jhead  -autorot -ft -nf%y%m%d-%H%M%S $SortLocation/*

###########
# Sort files into folders using $CharFromName letters of the file name
#
ls $SortLocation | while read file; do
 # extract first $CharFromName characters from filename
 YYMM=${file:0:$CharFromName}
 # create directory if it does not exist
 test -d $SortLocation/$YYMM || mkdir $SortLocation/$YYMM
 # move the current file to the destination dir
 mv -v $SortLocation/$file $SortLocation/$YYMM/$file
done

##########
# move sorted files into photo library
mv -v $SortLocation/* $PhotoLibrary/
would it be possible to set $CameraPath to either the string set in the script, or the first argument used when running it. ($1)?

So I could execute the script as ./ImageSort.sh /new/location/of/images
to use /new/location/of/images as the camera path, but if I don't specify an argument it just uses the value given inside?

Also, can I have it pause when run, before moving the files to the sort location, and echo $CameraPath for confirmation?

Thanks again.
 
Old 05-01-2009, 10:39 AM   #4
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

Quote:
Originally Posted by SuperJediWombat! View Post
...would it be possible to set $CameraPath to either the string set in the script, or the first argument used when running it. ($1)?
Code:
CameraPath=/path/to/default
test -n "$1" && CameraPath="$1"
Quote:
Originally Posted by SuperJediWombat! View Post
Also, can I have it pause when run, before moving the files to the sort location, and echo $CameraPath for confirmation?
Code:
read -p "Path: $CameraPath; continue (y/n)?" confirm
test "$confirm" = "y" || exit 0
Jan
 
Old 05-01-2009, 12:38 PM   #5
SuperJediWombat!
Member
 
Registered: Apr 2009
Location: Perth, Australia
Distribution: Ubuntu/CentOS
Posts: 208

Original Poster
Rep: Reputation: 51
Moin,

Thanks jan, it's all coming together. I've just found that when using mv to move a directory, it does not merge the folders if the target directory has files inside.

Code:
##########
# move sorted files into photo library
mv -v $SortLocation/* $PhotoLibrary/
Gives this error
Code:
mv: cannot move `/home/angus/.imagesort/0905' to `/home/angus/Photos/0905': Directory not empty
Checking the mv man pages, and google hasn't helped, is there an argument to fix that?

Last edited by SuperJediWombat!; 05-01-2009 at 12:39 PM.
 
Old 05-02-2009, 02:15 AM   #6
SuperJediWombat!
Member
 
Registered: Apr 2009
Location: Perth, Australia
Distribution: Ubuntu/CentOS
Posts: 208

Original Poster
Rep: Reputation: 51
Finished

All finished.

Code:
#!/bin/bash
#
#
PhotosPath="/media/4GBSD/DCIM/101CANON"
SortPath="/home/angus/.imagesort"
LibraryPath="/home/angus/Photos"
CameraPath="/media/4GBSD"
CharFromName=4
echo 
echo 
############
# Test to see if $PhotosPath exists, if not promp for new path / exit.
test -d $PhotosPath || read -p "$PhotosPath does not exist, close to exit or type new path:" PhotosPath
test -d $PhotosPath || "read -p '$PhotosPath is invalid. Press enter to close' && exit"

############
# move files from camera to $SortPath
mv $PhotosPath/* $SortPath/

############
# rename all image files in $SortPath
# FolderDateDD-HHMMSS.ext
jhead  -autorot -ft -nf%y%m%d-%H%M%S $SortPath/*

###########
# Sort files into folders using $CharFromName letters of the file name
#
ls $SortPath | while read file; do
 # extract first $CharFromName characters from filename
 FolderDate=${file:0:$CharFromName}
 # create directory if it does not exist
 test -d $LibraryPath/$FolderDate || mkdir $LibraryPath/$FolderDate
 # move the current file to the destination dir
 mv -v $SortPath/$file $LibraryPath/$FolderDate/$file
done

##########
# move sorted files into photo library
#mv -v $SortPath/* $LibraryPath/ 

##########
# Umount the card
umount $CameraPath

##########
# End notification
echo 
echo "Photos  from: $PhotosPath"
echo "End location: $LibraryPath"
echo 
echo "The card has been ejected."
echo 
read -p "Press enter to close this window…"
 
Old 05-16-2009, 08:03 AM   #7
manville
LQ Newbie
 
Registered: May 2009
Posts: 4

Rep: Reputation: 0
Simplifying

A lot of effort gone into this script but it also also useful to note that jhead as used here can be used to move the photos directly into directories as it renames them by modifying the output string format:

e.g.:

jhead -n%Y/%m/%d/%f *.jpg

will put the photos into a directory structure of /year/month/day (I used %f to retain the original filename and there are differences between -n and -nf so check the manual).

(Also works in "the other os" but you need to use "\" not "/" for directories)
 
Old 05-16-2009, 06:53 PM   #8
SuperJediWombat!
Member
 
Registered: Apr 2009
Location: Perth, Australia
Distribution: Ubuntu/CentOS
Posts: 208

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by manville View Post
jhead -n%Y/%m/%d/%f *.jpg
Wow, thanks Manville. jhead is amazingly useful.
 
  


Reply

Tags
bash, date, exif, photos, sort, sorting



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
LXer: Sorting Photos LXer Syndicated Linux News 0 01-05-2008 03:00 AM
Sorting files by time / date. TheRealDeal Linux - General 2 08-06-2006 09:35 PM
need software to batch-rotate jpeg's based on exif data mfcarroll Linux - Software 3 03-07-2005 02:39 PM
Any programs to rename a image to it's EXIF date ? CRego3D Linux - Software 1 11-08-2004 02:25 PM
Sorting mail into folders? cli_man Linux - Software 4 11-07-2003 10:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:52 PM.

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