#!/bin/bash checkcommand() { local result local command="$1" which "$command" 1>/dev/null 2>&1 if [[ $? == 0 ]]; then echo "$command requirement OK" result=1 else echo "$command requirement MISSING" result=0 fi return $result } help() { echo "Usage: tphoto [ -h | -f ] [ *.jpg ]" echo "Rename jpg files using Exif Original Date/Time if exists." echo " -h Display help." echo " -f Force rename jpg files with Exif datetime or file datetime." } doRename() { local filename=$(basename $1) #echo "$filename" jhead -nimg%Y%m%d-%H%M%S "$filename" } doRenameCarefully() { local filename=$(basename $1) #echo "$filename" if [[ $filename =~ IMG[12][90][0-9][0-9][01][0-9][0-3][0-9][012][0-9][0-5][0-9][0-5][0-9].* ]]; then local target=img${filename:3:8}-${filename:11} if [ -e "$target" ]; then echo "$filename -r> $target ABORT because file already exists." else echo "$filename -r> $target" mv "$filename" "$target" fi elif [[ $(exiftool $filename | grep -c "Date/Time Original") > 0 ]]; then #echo "yop $filename" doRename "$filename" else echo "$filename IGNORED" fi } # main checkcommand "exiftool" if [[ $? == 0 ]]; then exit 1 else checkcommand "jhead" if [[ $? == 0 ]]; then exit 1 fi fi if [[ $# == 0 ]]; then find . -maxdepth 1 -type f -iname "*.jpg" | while read path do doRenameCarefully "$path" done elif [[ "$1" == "-h" ]]; then help elif [[ "$1" == "-f" && $# == 1 ]]; then find . -maxdepth 1 -type f -iname "*.jpg" | while read path do doRename "$path" done elif [[ "$1" == "-f" && $# > 1 ]]; then for filename do if [[ "$filename" != "-f" ]]; then doRename "$filename" fi done else for filename do doRenameCarefully "$filename" done fi