LinuxAnswers - the LQ Linux tutorial section.
Go Back > Blogs > jere21
User Name


Rate this Entry

Automatically set file date from EXIF data

Posted 12-27-2010 at 02:54 PM by jere21
Updated 12-27-2010 at 02:59 PM by jere21

You can set the creation date of a picture or video as its access and modification time: If the creation date was saved in the exif data of some media, you can extract it with "exiftool -ee -CreateDate" and then "touch" the file with that time.

To do this automatically save the following script e.g. as /usr/local/bin/exiftool_set_create_date
# exiftool_set_create_date: set the file date from exif "Creation Date".
echo -n "$@:  "

# This script takes one filename as option.
# Escape space in filenames.
# Other special chracters might be handled here, too.
	echo $@ | 
	sed "s| |\\ |g"

# Get the date from the exif data.
	exiftool -ee -CreateDate "$FILE" |
	grep -Eo "[[0-9]{1,4}:]?[0-9]{2}:[0-9]{2} [0-9]{2}:[0-9]{2}:[[0-9]{2}]?" |
	sed "s|\:||g" |
        sed "s| ||g"

# Convert it for "touch". Expected format is [[CC]YY]MMDDhhmm[.ss]
[ -n "$DATE" ] &&
	if [ "$( echo "$DATE" | wc -m )" -eq 15 ] ; then
		DATE_TOUCH="$( echo "$DATE" | grep -Eo "^[0-9]{12}").$( echo "$DATE" | grep -Eo "[0-9]{2}$" )"
	elif [ "$( echo "$DATE" | wc -m )" -ge 9 ] && [ "$( echo "$DATE" | wc -m )" -le 15 ] ; then

# Set the date on the file.
[ -n "$DATE_TOUCH" ] &&
	echo -n " touching with $DATE_TOUCH" &&
	touch -t $DATE_TOUCH "$FILE" &&
	echo " ...done." &&
	echo "$FILE" >> ~/"$(basename $0)".log ||
	echo " ...failed."
	echo "$FILE" >> ~/"$(basename $0)"_fail.log

Don't miss that the script has to be executable:
chmod +x /usr/local/bin/exiftool_set_create_date
Usage: exiftool_set_create_date FILE

To change the file date of all files in the current directory:
find . -type f -exec exiftool_set_create_date '{}' ';'
You will get some output and separate logfiles for both successfully touched files and failed files in your home directory.
Posted in Uncategorized
Views 1823 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 05:17 AM.

Main Menu

Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration