I just read Tantes post regarding his script to rename pictures using the exif data written by the camera.
So I want to present my own script too. It works nearly the same way as Tantes but this script adds a check for existing pictures and not existing exif data.
See the script below the break.
#!/bin/bash
######
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; version 2 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
######
######
## This script requires the ubuntu package "exiftags". Other distros might
## provide it with another name but it have to provide the command "exiftime"
######
# Hereingegebene Datei in $INFILE laden
INFILE=$1
# Sollte die Datei nicht existieren mit Status 1 aussteigen
if ! [ -e "$INFILE" ]
then
exit 1
fi
# Die Dateiendung des Opfers ermitteln und dafür sorgen, dass sie lower-case ist
TMP=$(echo "$INFILE" | sed "s/.*\.\([a-zA-Z]*\)$/\1/")
SUFFIX=$(echo $TMP | sed "y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/")
# Das Zielverzeichnis ermitteln
SUBDIR=$(echo "$INFILE" | sed "s/^\(.*\/\)[^\/]*$/\1/")
# Aus den Exif-Tags den neuen Dateinamen ermitteln
NEWNAME=$(exiftime -tc "$INFILE" | sed 's/.*: \([0-9].*\)/\1/;s/:/-/g;s/ /_/g')
# Sollte es nicht möglich sein die Exif-Daten auszulesen dann Status 2 und raus.
if ( test "$NEWNAME" == "" )
then
exit 2
fi
# Standardmäßig den blanken Dateinamen ohne Counter verwenden
RIGHTNAME=$NEWNAME
# Counter initialisieren
CT=0
# Sollte es schon eine Datei mit genau diesem Namen geben wird ein Counter angehängt
while [ -e $SUBDIR$RIGHTNAME.$SUFFIX ]
do
CT=$(( $CT + 1 )) # Den counter erhöhen
RIGHTNAME=$NEWNAME"_"$CT # Den counter an den Dateinamen anhängen
done
# Schlussendlich die Datei in ihren neuen Namen umbenennen
mv --update "$INFILE" $SUBDIR$RIGHTNAME.$SUFFIX
# Den Rückgabestatus des mv zurück liefern
exit $#