Rename file to include the date and time from its modified date

From WickyWiki


Set modification date and time from the filename of the form "yyyyMMdd-hhmm*.jpg" or "yyyyMMdd-*.jpg", this script returns the statements.

#!/bin/bash
for file in *.jpg
do
	year=${file:0:4}
	month=${file:4:2}
	day=${file:6:2}
	hour=${file:9:2}
	minute=${file:11:2}
	if ! [ "${hour}${minute}" -eq "${hour}${minute}" ] 2> /dev/null
	then
		hour="00"
		minute="00"
	fi
	if ! [ "${year}${month}${day}" -eq "${year}${month}${day}" ] 2> /dev/null
	then
		echo "#'$file' not containing a date"
	else
		#modify exif metadata
		echo "touch -t \"${year}${month}${day}${hour}${minute}\" \"${file}\""
	fi
done

Rename file from EXIF metadata 'picture taken' time

Rename files "file.jpg" to "YYYYMMDD-HHMM-file.jpg", using 'picture taken' time:

jhead -n%Y%m%d-%H%M-%f *.jpg

See also