5

I have a lot of pictures with the wrong creation date and wrong modification date. There is however a third date, "content created" that is correct. user3439894 had this command line as an answer to Change file creation date to modification date using terminal

for f in *.[iI][mM][gG]; do m="$(GetFileInfo -m "$f")"; SetFile -m "$m" -d "$m" "$f"; done

Where GetFileInfo reads the modified date and rewrites the created date. Very useful and works great (after installing the command line tools) for a lot of my pictures that have the correct modification date, thanks. What I am looking for now, is a syntax for "content created" date that I could use?

Any help on this would be great.

1

3 Answers 3

5

Using tools that are part of macOS, We start by getting the ContentCreationDate from the file and place it into a container ccd -

ccd=$(mdls -raw -n kMDItemContentCreationDate FILE)

Next, We format the ContentCreationDate string into a string usable by SetFile and place that into a container nct -

nct=$(date -f '%F %T %z' -j "$ccd" '+%D %T %z')

Finally, We use SetFile and set the FSCreationDate to the modified date/time string from ContentCreationDate -

SetFile -d "$nct" FILE

I'm assuming that you know how to place all of this into a loop for processing multiple files.

Note: Look into exiftool

2
  • Smart, here’s how to do the core part, well done and super clear answer.
    – bmike
    Aug 2, 2020 at 8:50
  • 3
    Thanks a lot! I am a bit of a novice in Terminal, but followed up you advice on exiftool, which proved to be very good! this simple line did the job: exiftool "-FileModifyDate<DateTimeOriginal" *
    – Arne Crogh
    Aug 2, 2020 at 14:41
2

As you mentioned, this command works great to automatically convert both the "File Creation" and "File Modification" dates to the "Content Created" date.

exiftool "-FileModifyDate<DateTimeOriginal" *

But a little warning to those unfamiliar with the terminal, this command will do so to every file in the user directory by default. If someone has a specific folder they want to run this on, better to first use the "cd" command into the folder they want.

Alternately, you can just paste (not hit enter, just paste) the command without the asterisk into a terminal window:

exiftool "-FileModifyDate<DateTimeOriginal"

And before running the command, go back to Finder, select the files you want, and literally drag the files into the terminal window. It will auto-populate with the lists of files and then you can run the command, only affecting the files you chose.

1

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"
}
3
  • 1
    How does this read existing exif data from the file and use it in the update?
    – nohillside
    May 28, 2022 at 7:33
  • It doesn't. To do that, you'll need exiftool(1) for that. Type "man exiftool" to see more than you ever wanted to know about exiftool. Type "which exiftool" to see if it exists on your system — I don't think it comes with it. If you don't have it, it is best to get it via a package manager — my favourite is homebrew. Easy install directions for homebrew are at: brew.sh. After you have homebrew installed, you can type "brew install exiftool", and Bob's your uncle! May 28, 2022 at 16:08
  • I just added some instructions for exiftool and GraphicConverter to the answer above. May 28, 2022 at 16:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .