title: Bash script to read EXIF, rename JPG files and check output if file exit -> create newfile-01.jpg
body: <p><strong>Update: 10.12.2014 - 
  Fixed!!
  Script finds all jpg,jpeg,JPG files in folder+all_subfolders and sets file modification timestamp=exif date/time.
  +it also writes the size of image into the filename too. (neat to know pixel size of image by just looking at the filname)</strong></p>
  
  <p>Original:
  I been working on a script for long time, picking up info traces of info and code to use.
  I have managed to make a script. Run it in a folder, and it will pick up all .jpg .JPG in current and subfolders and rename them to filename. i.e:  <em>2014_10_19_12_24_05_DSC-RX100_F5.6_f37.1mm_1T250s_ISO320.jpg</em></p>
  
  <p><strong>Problem</strong><br>
  The photos may have been taken at the same time with fast shutter speed. For example:<br>
  2014_10_19_12_24_05_DSC-RX100_F5.6_f37.1mm_1T250s_ISO320.jpg  (original filename P00002727.jpg)<br>
  2014_10_19_12_24_05_DSC-RX100_F5.6_f37.1mm_1T250s_ISO320.jpg  (original filename P00002728.jpg)  </p>
  
  <p>The first file is written okay, and then the <em>second file overwrites the first one</em> :-/</p>
  
  <p><strong>If someone could help</strong><br>
  I would like the script to have a "if file exist, the new filename should be renamed to having an increased number 01,02,03..etc.." at the end of the filename. Like this:</p>
  
  <p>2014_10_19_12_24_05_DSC-RX100_F5.6_f37.1mm_1T250s_ISO320.jpg<br>
  2014_10_19_12_24_05_DSC-RX100_F5.6_f37.1mm_1T250s_ISO320_01.jpg   (added _01,02,03 etc..)</p>
  
  <p>The script I'm using:</p>
  
  <pre><code>#!/bin/bash
  
  #extensions="jpg,jpeg,png,gif,psd,bmp,crw,thm,tif,tiff"
  fileTypes="jpg,jpeg,JPG"
  
  #make regex to find files with extensions in $fileTypes
  fileTypes=".*\.\(${fileTypes//,/\|}\)"
  
  # loop through all the image files
  find . -iregex "$fileTypes" -print0 | sort | while read -d "" s
  #find . -iregex '.*\.\(jpg\|JPG\|jpeg\)$' -print0 | while read -d $'\0' s
  #find . -name "*.jpg" -print0 | while read -d $'\0' s
  
  #find . -type f -iname "*.jpg" -print0 | while read -r -d $'\0' s ;
  
  #find . -iname "*.jpg" -print0 | while read -d $'\0' s ;
  
  
  #for s in $(find . -iname *.jpg -print0 | while read -d $'\0' s);
  do
  echo "------------  Start --------------------------"
  
  echo ""
  let counting=counting+1
  echo "Number of files: $counting"
  
  ## skip files that already contain _XX increment
  #ffn="${s##*/}"                                        # full filename w/o path
  #fn="${ffn%.*}"                                        # split filename from ext
  #if [ "${fn: -3:1}" = '_' ]; then                      # have we added '_XX' before ?
  #    continue                                          # skip to next file 's'
  #fi
  
  header=`jhead "$s"`                                     # header holds jhead info about
  justFileName=`basename "$s" | sed 's/\(.*\)\..*/\1/'`
  dateTime=`echo "$header" | grep "Date/Time"`            # dateTime fetches Date Time line
  fileDate=`echo "$header" | grep "File date"`
  
  path="`pwd "$s"/ `"
  file="`echo "$s" | sed 's/^.//'`"    # . = 1. first chars is removed in front
  pathwithfile="`echo $path$file`"     # does not lookup directory with spaces
  pathwithspaces="$PWD$file"           # does lookup direcotyr with spaces!
  justPath=`dirname "$pathwithfile"`
  
  exifdateTime=`echo $dateTime | awk -F ":" '{print $2"_"$3"_"$4"_"$5"_"$6}' | awk -F " " '{print $1"_"$2}'`
  
  dateFileTime=`echo $fileDate | awk -F "_" '{print $1"_"$2"_"$3"_"$4"_"$5"_"$6}' | awk -F ":" '{print $2"_"$3"_"$4"_"$5"_"$6}'`
  #| cut -c 16-34`
  #datefileName=`echo $justFileName | cut -c 1-19`
  
  
  
  FULLPATH="$pathwithspaces"
  # remove all the prefix until "/" character
  FILENAME=${FULLPATH##*/}
  # remove all the prefix unitl "." character
  FILEEXTENSION=${FILENAME##*.}
  # remove a suffix, in our cas, the filename, this will return the name of the directory that contains this file
  BASEDIRECTORY=${FULLPATH%$FILENAME}
  
  #echo "FULLPATH = $FULLPATH"
  #echo "FILENAME = $FILENAME"
  #echo "FILEEXTENTION = $FILEEXTENSION"
  #echo "BASEDIRECTORY = $BASEDIRECTORY"
  
  
  #echo "dateTime: $dateTime"
  #echo "exifdateTime: $exifdateTime"
  #echo "fileDate: $fileDate"
  #echo "dateFileTime: $dateFileTime"
  #echo "pathwithfile: $pathwithfile"
  #echo "pathwithspaces: $pathwithspaces"
  
  
  
  stripedfilename=`echo $FILENAME | cut -c 1-19`
  
  if [ $exifdateTime = $stripedfilename ]; then
  echo "File \"$s\" already exists with exifdateTime: $stripedfilename"
  echo "..skip to next file"
  echo "--- End ---"
  
  continue
          stripedfiledate=$fileDate | cut -c 1-19
          if [ $stripedfiledate = $stripedfilename ] ; then
          echo "File \"$s\" already exists with fileDate: $stripedfiledate"
          echo "..skip to next file"
          echo "--- End ---"
          continue
          fi
  fi
  
  #echo "break"
  #break
  
  
  
  
  #if [ -z "$dateTime" ] || [[ $dateTime == "Date/Time    : 0000:00:00 00:00:00" ]];
  if [ "$dateTime" = "Date/Time    : 0000:00:00 00:00:00" ]
  then
  
          if [ -z "$fileDate" ]
          then
                  # If Date/Time=0000:00:00 check File date
                  dateTime=`echo "$header" | grep "File date"`
  #               echo "Date/Time(ny): $fileDate"
  
                  # Set timestamp from exif
  #               echo "File date(ny): $exifdateTime"
                  jheadrun=`jhead -autorot -exonly -ft -norot "$FULLPATH"`
  
          else
                  # Set exif = modified timestamp.
                  jheadrun=`jhead -dsft "$FULLPATH"`
  
  #echo "..fixed Date/Time:   0000..."
  #echo "break 1"
  #break
  
          fi
  
  
  
  # Set new dateTime
  header=`jhead "$s"`                                     # header holds jhead info about
  dateTime=`echo "$header" | grep "Date/Time"`
  #justFileName=`basename "$s" | sed 's/\(.*\)\..*/\1/'`
  exifdateTime=`echo $dateTime |  awk -F ":" '{print $2"_"$3"_"$4"_"$5"_"$6}' | awk -F " " '{print $1"_"$2}'`
  
  
  fi
  
  
  # if -z variable is zero
  # if -n variable is none-zero
  
  if [ "$fileDate" = "" ] ;
  then
  #echo "break 2"
  #break
  
  #echo "breake 2 - True"
          # Set exif = modified timestamp.
          jheadrun=`jhead -dsft "$FULLPATH"`
  #       echo "jhead -dsft $jheadrun"
  else
  #echo "break 2 - False"
          # Set Date/Time from File-modification timestamp
  #        echo "Date/Time(ny): $exifdateTime"
          jheadrun=`jhead -autorot -exonly -ft -norot "$FULLPATH"`
  #       echo "`jhead -autorot -exonly -ft -norot \"$FULLPATH\"`"
  
  fi
  
  
  
  
  #
  # check file do have Modification date
  # if yes
  # set Date/Time = modification date
  #
  if [ "$dateTime" = "" ] ;
  then
  #echo "break 3"
  #break
  
  echo "breake 3 - True"
          # Set exif = modified timestamp.
          jheadrun=`jhead -mkexif -dsft "$FULLPATH"`
          echo "jhead -mkexif -dsft $jheadrun"
  fi
  
  
  
  if [ "$exifdateTime" != "$dateFileTime" ]
  then
  
  #echo "break 4"
  #break
  
  
  
  # echo $s
   x=`jhead "$s" | \
   awk 'BEGIN { cmt=""; }
  #/File name/      { n=$4; gsub(".jpg","",n);}
  /Camera model/   { c=$4$5$6;}
  /Exposure time:/ { e=$3;
                     if (e==int(e)) e=int(e);
                     if (e&lt;1) {e=int(0.5+1/e); e="1T" e "s";} else { e=e "s"; }
                   }
  /ISO equiv./     { iso="ISO" $4; }
  /Focal length/   { f="f" $4; }
  /Date.Time /     { d=$3 "_" $4; gsub(":","_",d); }
  /Resolution /    { size=$3$4$5$6; }
  /Aperture /      { ap=$3; gsub("f/","F",ap); }
  /Comment  /      { cmt=$3 "_"; }
  END { print d "_" c "_" size "_" ap "_" f "_" e "_" iso ".jpg"; }'`
  
  
  commentcheck=`echo "$header" | grep Comment | awk -F ":" '{print $2}'`
  
  
  if [ "$commentcheck" != " Original_filename" ]
  then
  #echo "break 5"
  
  comment="Original_filename: $FILENAME"
  commentcommand="jhead -cl \"$comment\""
  
  
  echo "..no comment in file, adding; $comment  &lt;-- adding comment"
  
  #echo "commment: $comment"
  #echo "commentcheck: $commentcheck      &lt;-- if this is blank file does not have comment"
  #echo "commentcommand: $commentcommand"
  
  addcomment="$commentcommand \"$pathwithfile\""
  #addcomment=("jhead -cl $comment $pathwithfile")
  
  #echo "addcomment: $addcomment"
  
  eval $addcomment  # Run variable in terminal
  
  else
  continue
  
  fi
  
  #echo "break 6"
  #break
  
  ########
  
  
  
  PADDING=2
  NEWFILE="$BASEDIRECTORY$x"
  
  if [[ -f $NEWFILE ]]; then
      BASE=`echo $x | sed "s/\.jpg/_/"`
  #echo $BASE
      LAST=`ls -1 "$BASEDIRECTORY" | grep $BASE | sort -r | head -1`
  #echo $LAST
      LASTNUM=`echo ${LAST:${#x}-3:$PADDING} | sed "s/^0*//"`
  #echo $LASTNUM
      let LASTNUM=LASTNUM+1
  #echo $LASTNUM
      FINAL=$BASE$(printf "%0"$PADDING"d.jpg" ${LASTNUM})
  #echo $FINAL
      NEWFILE=$BASEDIRECTORY$FINAL
  fi
  
  
  mv "$s" "$NEWFILE"
  #NODUPLICATE="jhead -n%f-%02i \"$pathwithspaces\" \"$NEWFILE\""
  #echo "mv \"$pathwithspaces\" \"$NEWFILE\""
  
  echo "New filename: $NEWFILE"
  
  
  
  #echo "---"
  #header=`jhead "$NEWFILE"`
  #echo "$header"
  #echo "---"
  
  #echo "break 6"
  #break
  
  #echo "$s"
  
  fi # END OF if [ "$exifdateTime" != "$datefileName" ]
  
  unset x
  #echo "$x"
  
  #echo "Sleeping 1/100 of a second"
  sleep 0.01
  #sleep 0.05
  #sleep 3
  
  echo "End..------------------------------------------------"
  echo ""
  done
  </code></pre>