LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Blogs > jere21
User Name
Password

Notices


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
Code:
#!/bin/sh
# 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.
FILE="$(
	echo $@ | 
	sed "s| |\\ |g"
)"

# Get the date from the exif data.
DATE="$(
	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
		DATE_TOUCH=$DATE
	fi

# 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
	}

exit
Don't miss that the script has to be executable:
Code:
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:
Code:
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 3239 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



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

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