Extract the Exif DateTimeOriginal field and prepend it to the file name to facilitate image sorting

  • Posted on: 19 May 2014
  • By: Michael

Clients!? Clients are wonderful things. They pay the bills. But they definitely ask you for some odd things.

I have a client who needed 10s of thousands of images, from different sources, to be displayed by the date they were taken. As he didn't want to spend hours in his favorite image editor, checking the properties of each file one by one, he punted the whole thing to me with a: "Hey, take these images, and add them to the gallery and make sure they display by date."

As it's my business to make clients happy, my reply, "Sure. I'll have that for you in a few days."

Luckily, he created most of the files, so the Exif data was intact. That being so, I wrote a throw away script to sort the mess out.

Here's the script, and don't blame me if it breaks something :)

#  Extracts the Exif:DateTimeOriginal field and prepends
#  it to the image file to facilitate sorting.
#  One off script, possibly has bugs, use at your own risk.
#  You will need imagemagick:
#    yum install imagemagick
#    apt-get install imagemagick
#    (whatever you use to install it)
# Script=addexiftime
# Author=Michael
# Email=staff_scripts at the commercial domain inet-design
# Website=inet-design.com
# License=GPL
# Script repository=
# # # #
#   Prettify things
#  Who am I?
MyName=`basename "$0"`
#  Check usage
if [ "$1" != "" ] && [ ! -d "$1" ] ; then
  echo -e "$YELLOW_F""Usage               :""$NORM  $GREEN_F""$MyName [\"Path\"] [\"File Filter\"]""$NORM"
  echo "Default Path        :  Current Directory"
  echo "Default File Filter :  *.jpg"
  echo -e "$YELLOW_F""Example             :""$NORM  $GREEN_F""$MyName \"/home/user/some directory\" \"*.jpeg\"""$NORM"
#  Say Hello
StartTime=`date +%s`
CurrDateTime=$(date +"%y-%m-%d %H:%M:%S")
echo -e "$RED_F$CurrDateTime:$GREEN_F$BOLD Starting$NORM \"$MyName\" It can take awhile, need coffee?"
#  Do the work
if [ "$1" != "" ] ; then
  cd "$1"
if [ "$2" != "" ] ; then
FileList=`find ./ -maxdepth 1 -type f -name "$FileExt"`
FilesToProcess=`echo "$FileList" | wc -l`
D00=`date +"%y-%m-%d %H:%M:%S"`
MESG="Adding EXIF timestamps and copying to: $ResultsDir"
MESG="Files to Process : $FilesToProcess"
mkdir -p results
while IFS= read -r filename
  NameofFile=`basename "$filename"`
  DateString=`identify -verbose "$NameofFile" | grep exif:DateTimeOriginal | awk '{ print $2" "$3 }' | sed -e s/":"/"."/g -e s/" "/"."/g`
  if [ "$DateString" = "" ]; then
  cp "$filename" "results/$DateString-$NameofFile"
  FilesProcessed=$((FilesProcessed + 1))
  D00=`date +"%y-%m-%d %H:%M:%S"`
  MESG="Files Processed  : $FilesProcessed"
  echo -en "$YELLOW_F$D00$NORM - $GREEN_F$MESG$NORM"
  echo -en "\r"
done <<< "$FileList"
D00=`date +"%y-%m-%d %H:%M:%S"`
MESG="Files Processed  : $FilesProcessed"
D00=`date +"%y-%m-%d %H:%M:%S"`
MESG="Files in $ResultsDir : `ls -1 "$ResultsDir" | wc -l`"
#  Say Goodbye
END_TIME=`date +%s`
ELAPSED=`expr $END_TIME - $StartTime`
remainder="$(expr $ELAPSED % 3600)"
hours="$(expr $(expr $ELAPSED - $remainder) / 3600)"
seconds="$(expr $remainder % 60)"
minutes="$(expr $(expr $remainder - $seconds) / 60)"
CurrDateTime=$(date +"%y-%m-%d %H:%M:%S")
echo -e "$RED_F$CurrDateTime:$GREEN_F$BOLD Finished$NORM \"$MyName\"  Elapsed time: `printf %02d $hours`:`printf %02d $minutes`:`printf %02d $seconds`"

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
You know what to do...
5 + 6 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.