#!/usr/bin/env bash # HEADER #======================================================================= # # img2date -- Shell script to rename images by their creation date stamp # # SYNOPSIS # img2date # # DESCRIPTION # Shell script to rename all images in a given directory by their # creation date stamp of the form `YYYY-MM-DD_hh-mm-ss.{jpg,png}`. # # EXAMPLE # $ img2date # # POSITIONAL ARGUMENT # directory where are the image collection # # DEPENDENCIES # - exiftool # - GNU coreutils # # INSTALLATION # - link file in $PATH (~/bin/) # # # Copyright (C) 2023 Nicolas Mesnier # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License version 3 or # above as published by the Free Software Foundation. # # 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. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # #======================================================================= # END_OF_HEADER #----------------------------------------------------------------------- # *** read this header to display help and version #----------------------------------------------------------------------- BOH=$(head -200 ${0} | grep -n "^# HEADER" | cut -f1 -d:) EOH=$(head -200 ${0} | grep -n "^# END_OF_HEADER" | cut -f1 -d:) function Help(){ head -$(($EOH-1)) ${0} | tail -$(($EOH-$BOH-1)) \ | grep -e "^#$" -e "^# " | sed -e "s/^#=*//g" } #----------------------------------------------------------------------- # *** run #----------------------------------------------------------------------- DIRECTORY=${1} for IMG in $(find ${DIRECTORY}/ -regextype sed -regex ".*/*\.\(jpe\?g\|JPE\?G\|png\|PNG\)" | sort);do DATE=$(exiftool -T -createdate ${IMG}) if [ "${DATE}" == "-" ]; then DATE=$(exiftool -T -modifydate ${IMG}) fi if [ "${DATE}" != "-" ]; then case "$(exiftool -T -filetype ${IMG})" in "JPEG" ) EXT=".jpg";; "PNG" ) EXT=".png";; esac NAME=${DIRECTORY}/$(echo ${DATE} | tr ":" "-" | tr " " "_" )${EXT} mv ${IMG} ${NAME} fi done #--------------------------------------------------------------------eof