And for something totally different that uses only basic commands, you can do this:
# mv OriginalFileName.jpeg TempFileName.jpeg
# touch -t 199912310000 OriginalFileName.jpeg
# cat TempFileName.jpeg > OriginalFileName.jpeg
The basic system commands don't really want you messing with the creation date, because, well, it is the creation date, which should really be considered immutable!
(Why use touch
when there are good examples here using SetFile
? Well, the dates used by SetFile
must be in the "Unix Epoch," which means they must be between 1970-01-10 and 2038-01-18. If that works for you, fine, but I have a lot of scanned photographs and PDF documents from before 1970, and touch
will set creation dates outside of the UNIX Epoch, back to 1900. In addition, the formatting is tricker for SetFile, which does not use ISO Standard date/time formatting.)
So this solution basically copies the file, creates a file at a given point in time, then copies your original file's contents into the created empty file.
You can easily package this into a shell function and store in your .bashrc file for easier use:
function recreate {
mv "$2" /tmp/file_to_recreate
touch -t $1 "$2"
cat /tmp/file_to_recreate > "$2"
}
Now you can simply type recreate 199912310000 MyPhoto.jpg
and your function will "re-create" the file with the desired creation date.
The touch
command with the parameter "-t" takes a date time in the following format: YYYYMMDDHHmm. You must enter all characters. Non-existent dates are silently accepted without error, and produce unpredictable arbitrary dates.
I use this for photographs, yes, but also I set the creation date to the copyright date on PDF books I collect.
I have a lot of scanned slides and photographs that don't have an internal EXIF creation date, so I haven't included updating EXIF data from the file creation date in my function.
Instead, I do the opposite: I use GraphicConverter, which has the best access to EXIF/IPTC data I've seen in any regular (non-Terminal) application. It can batch-set the EXIF internal creation date to the file creation date with just a couple mouse clicks.
Going the other direction — setting the file system creation date to the file's internal EXIF creation date — will require exiftool. This will allow you to extend the script above by obtaining the date you use in touch
with a date from the image file, as supplied by exiftool
.
To do that, replace the literal date string in the script above ("199912310000") with exiftool -createdate -d "%Y%m%d%H%M%S" -s3
, surrounded by back-quotes.
Here's a new function for you that I've done just a bit of testing on. It takes one argument: the name of the image file to re-date.
function SetCreateFromExif {
mv "$1" /tmp/file_to_re-create
touch -t `exiftool -createdate -d "%Y%m%d%H%M.%S" /tmp/file_to_re-create` -s3 "$1"
cat /tmp/file_to_re-create > "$1"
}