How To Change File Creation Dates To Match Photo Metadata

When you export photos from the Photos app the date of the file doesn't match the date the photo was taken. This is correct as the file is new and the photo in it does retain the metadata with the original date. But if you want to force the file date to match the photo date, you can do it in the Terminal or with an Automator app.
You can also watch this video at YouTube.
Watch more videos about related subjects: Automator (50 videos).

Video Transcript

Hi, this is Gary with MacMost.com. Today I'm going to show you how to change the creation date of a file to match the photo date.
MacMost is brought to you thanks to a great group of more than 1000 supporters. Go to MacMost.com/patreon. There you can read more about the Patreon Campaign. Join us and get exclusive content and course discounts. 
Now I get a lot of questions about this particular problem that happens when you export a photo from something like the Photos App. So say you take a photo like this one. If I get information on it with Command i, I can see this photo was taken on May 27 at 6:59. If I drag and drop it to the Desktop to create a file I can select it, do Command i to get info in the Finder on it. I can see now the created date is June 14th at 4:01 which is right now. So what's going on here. Well, both are correct. This is the creation date for this file. I just created the file. The photo in it has metadata that is the date that the photo was taken. That's what we see here in the Photos App when we get info on it. Now you may think that by exporting the file it has wiped out that photo metadata. So it no longer remembers when the photo was taken. That's not true. If I double click on the photo it opens up in Preview. I go to Tools and then Show Inspector and then here under info exif I get the metadata and there is the date of the original photo. So it does remember the metadata. If I were to import this into another photo management app or back into photos it would use this date and time here as the date the photo was taken. So everything is fine and that may be all that you need to know. That the metadata is preserved. 
However some people would like the photo creation date to match the photo date. Like they took the photo and instantly put it onto their Mac. Now technically this isn't kosher. A file creation date should be the date that the file was created. If you want to cheat and change it you can though. So it's your file. If you want to do it here's how.
Let's start off in Terminal and I'm already here on the Desktop. So I've gone there. So I could see this file here. I could actually change the creation date using the Touch Command. Touch dash t changes the creation date. You have to use a weird format. It's the year as four digits and then the month, let's say January 31st, and then the hours as two digits. So let's say 13 for one o'clock and then thirty for 1:30. Then the file name. So I can start typing the file name and autocomplete it there. That would actually change the creation date for that file. However, I'm not going to do it here. The Touch Command is very temperamental. First of all it can only set a creation date if it's earlier than the current creation date of the file. So if this file had a creation date that was say 2020 and I tried to change it to 2021 it wouldn't work. Also if you tried to do it too much or even just a second time with the same file sometimes it doesn't work either. So it's kind of temperamental. macOS and the Finder are not built for having you manually change the  creation dates of files. It's kind of sacred to keep the file creation dates as they were.
The next thing we want to do is get the actual date of this file. MDLS will get us all of the metadata. So for this file I get a huge list of metadata. Way up here at the top of it I will see one item here that gives me the actual creation date. You can see that matches. Or it doesn't quite match. Notice the time is different. This has it at midnight. Midnight the next day. It's six hours ahead because there is a time difference. This is universal time. So plus is zero. Whereas I'm actually at plus six here in mountain time. We'll deal with that later. For now I'm going to go and pipe the results. Pipe is the character above the Return key on your keyboard. You have to use Shift and it's just a straight up and down line. That means send the results of this into something else. I'm going to do grep and I'm going to look for a line that starts with this. When I do that I get that creation date right here. I also get another line here, underscore Ranking. I don't want that. So I'm going to pipe yet again to head dash n1 which will say just give me the first line at the head of the results. There I get that. Now I'm going to pipe yet again a third time to awk which allows you to run more commands. I'm going to use this which will substitute digits, actually things that are not digits, I've got the not here and put a space there instead. In other words clear out everything except the digits with spaces in-between. So this is just going to create a bunch of numbers and I'm going to get the first number, second number, third, fourth, and fifth number and that will give me that. So instead of this here it's actually going to be taking just these numbers and it's going to just have the numbers. No punctuation or spaces in-between which is the correct format for that touch command. 
Okay, so the next thing I want to do with this is subtract that six hours to get the time right. So I want to use the Date command for that. So date and j and f and I want to give it a format to accept for input. So that would be this. So you can see the year, month, and day, hour, minute. This format here. I'm going to do dash v and subtract six hours. Capital H there to get the time right. So you're going to have to play around with this and see how far the times are off for your photos and adjust with either a plus or minus and a number with a capital H for the number of hours. Now I want to pass the time in there. So I going to do dollar sign and parentheses and this will make it evaluate what's inside the parentheses which is going to be all of this. So I'm going to paste it in and then close the parentheses. Now I want to output the time in the same format. So I'm actually going to take this format here and output it. So now you could see I get the time adjusted. Now it is 6:59. 
So now all we need to do is use this inside of Touch. So we'll do touch dash t and we will take this entire thing, copy it, but I'm also going to have to tell touch to evaluate that before using it. So put that inside a parentheses with a dollar sign in front, close it off like that, and then I want to have the file name again, so the same file name here, and this should change the date there. So let's go and bring up the info on this. You could see here it's June 14th. Let's run this. You could see, ah!, it changed it and got it right. Now let's put all of this inside an Automator script so we can run it whenever we want just by dragging it and dropping. 
So in Automator I'm going to create an Application. I'm going to simply look for the Shell Script here, run Shell Script. I'm going to say asking put as argument. This will put this handy little loop here for us. It's going to take the arguments and it's going to loop through each one. So if there are multiple files it will do the same thing for each one. It's going to store the name of the file in f and here it's just going to just print out the name of the file. That's not what we want. What we want is to use our Shell Script here. I'm going to grab this and paste it in. But I don't need the file name now. Instead I need this f right here. So dollar sign f. We have it actually in another place, dollar sign f. So now we'll save it and we'll save it to the Desktop. We'll say change photo file date and we can test it out.
We've got the app right there. Let's go to Photos and grab a couple of photos here. This one, this one, and this one. I'm going to drag them here. Great. Now, let's get info on all  three of these. We can see here that they are now set to the current time. That's not what we want. They should be set to the time when they were originally taken. What's  in the metadata. So let's drag and drop these all onto here. We can see here it changes them all. So, now we have a handy way to drag and drop any photo files we want on here to use this to set the file creation date equal to the photo date. It will work as long as we're doing it as pretty much the first thing we do with these files once we add them  to the Finder. Once we export them from the Photos App and we only try to do it once. Otherwise the Touch Command may not work as expected. 
I don't necessarily recommend doing this and I personally, when I export files, I don't try to use this. But if you're doing a one time thing where you need to export a bunch of files from a Photos Album and you want to set the dates right before you, say, put them on a USB stick and give them to somebody, then I can see possibly using this automated workflow to get it done. I'll include this bit of code here since it's pretty long and you wouldn't want to type it and you get one character wrong and it's just not going to work at all. It's got to be perfect and also make sure you're running this as a zShell bash script, not one of these other ones, or it may not work. You've remembered to accept the input as arguments and created an application here in Automator like this. It may also ask for permission to access file the first time you run this. But maybe when you run it in a new location as well.
 So I only recommend using this, or anything like this, if you really understand what it's doing and really need to accomplish this particular task of changing those file creation dates to match when the photos were actually taken. 

Here’s the code used in this tutorial:

for f in "$@"
do
	touch -t $(date -j -f "%Y%m%d%H%M" -v-6H $(mdls $f | grep kMDItemContentCreationDate | head -n1 | awk '{gsub("[^[:digit:]]+"," ");print $1$2$3$4$5}') +%Y%m%d%H%M) $f
done

Comments: 33 Comments

    Douglas Brace
    4 years ago

    Is it possible to do this so that the creation date is changed but not the modified date? Why am I asking? Technically speaking someone might want the creation date to be the actual original date and time from when the photo was taken but might want the modified date to be when the photo was exported (in this case, since the original was an HEIC file the export would be the JPEG version of it). If it is possible, thanks in advance. If not, I completely understand and I appreciate everything.

    4 years ago

    Douglas: That will take much more work. You'd have to save the original modified date to a variable, then recall it and set the modified date after setting the primary date.

    Dan
    4 years ago

    Amazing Thank You!
    Just what I was looking for. All other guides was only about changing the date manually one file at a time.
    With this Automator I can now change thousands of files by select and drag.

    iPhone Photo app seems not to care about true metadata, and uses the file created date instead, when transferring backed up photos and videos back to iPhone.

    Craig
    4 years ago

    Will this work for .mov files?

    4 years ago

    Craig: Theoretically yes. But video file metadata is different so you’ll need to research the correct things to address and perhaps do some trial and error.

    Victor
    4 years ago

    Hi,
    could you tell me why the renamed folder shows hour from metadata with 2 hours less than the real hour?
    In the Exif data from my photos (macOS Big Sur) it shows: "Date Time Original: 17 Oct 2020 at 18:56:49", and more below: "Time Zone for Original Date: +02:00". If I run your script it shows in the renamed folder name with 16:56:49".

    Thanks for answers/solutions.

    Kind regards

    Victor

    4 years ago

    Victor: I suppose because it is adjusting for time zone. Add 2 hours if you like.

    Rami
    4 years ago

    Hi - The challenge I have is, when I run the command (mdls) for my photo the result are:

    kMDItemContentCreationDate = 2017-11-02 20:07:18 +0000
    kMDItemContentModificationDate = 2018-12-17 21:07:18 +0000

    (kMDItemContentCreationDate) is not accurate, the (kMDItemContentModificationDate) is the accurate one.
    How I can update the file created date to match (kMDItemContentModificationDate) instated of (kMDItemContentCreationDate) ?

    Thanks for supporting

    4 years ago

    Rami: Did you try just changing the code to use the property you want?

    Rami
    4 years ago

    Yes, Here is the code I used,
    for f in "$@"
    do
    touch -t $(date -j -f "%Y%m%d%H%M" -v-8H $(mdls $f | grep kMDItemContentModificationDate | head -n1 | awk '{gsub("[^[:digit:]]+"," ");print $1$2$3$4$5}') +%Y%m%d%H%M
    202105271859) $f
    done

    However, the code above changed the "file modification date" not the "creation date" any thoughts?
    Also, any thoughts on why the kMDItemContentCreationDate does not match kMDItemContentModificationDate?

    4 years ago

    Rami: Not sure why it isn't working for you. Just keep researching and debugging. I don't have any more advice off the top of my head right now on this.

    Paweł
    4 years ago

    I used this script many times, but now I have an error "touch: out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]”.

    the script:
    for f in "$@"
    do
    touch -t $(date -j -f "%Y%m%d%H%M" -v+1H $(mdls $f | grep kMDItemContentCreationDate | head -n1 | awk '{gsub("[^[:digit:]]+"," ");print $1$2$3$4$5}') +%Y%m%d%H%M) $f
    done

    I don't now how to fix it?

    4 years ago

    Paweł: probably something up with the date of a file you are trying this with. Inspect those photo dates carefully or have it just echo the date in this script if you want (not sure if you are a coder or not).

    Russ
    4 years ago

    To fix the error: touch: out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]”

    Replace the curly quotes with the non-curly quotes.

    Paweł
    4 years ago

    I reset my computer and the error is gone.

    Dan
    3 years ago

    Hi again...
    I am having problem with not being able to set a creation date if it's earlier than the current creation date of the file.

    I have some files with the same creation date of 2000 but they are taken in 2020..
    kMDItemContentCreationDate = 2000-01-01 00:00:00 +0000
    kMDItemFSCreationDate = 2000-01-01 00:00:00 +000

    kMDItemContentModificationDate = 2020-08-29 16:12:00 +0000
    kMDItemFSContentChangeDate = 2020-08-29 16:12:00 +0000

    So is there really no way to set this right?

    3 years ago

    Dan: I think you should be able to. Experiment. You could always open them in an app and export out new files and try to change those.

    Elissa
    3 years ago

    I am following the video to a T, but on my macbook when I put "pass input" as "as arguments" I do not get the bits with an "f" instead it automatically shows this

    while ( $# )
    echo "$1"
    shift
    end

    I tried simply copy-pasting from your site into the automator but it says "illegal variable name" when I try to run it.

    3 years ago

    Elissa: Look at the Shell setting there too. Make sure it is the same as mine.

    Elissa
    3 years ago

    Thank you! It now shows the correct starting position. I copy pasted the script and tried to alter a photo but it only changed the modified time-stamp to two hours later from the created time. (Metadata is still untouched and safely preserved with the correct original date time though thankfully!)

    Elissa
    3 years ago

    Me again! It seems that if I run the files through them two or three times, then it works!! Some jpegs don't work, but I'll take it! -- Is there any way to do the same thing but with .mov or .mp4 files?? Thank you!!

    3 years ago

    Elissa: That's odd that it should take more than one time. Are you doing this in the Terminal, or Automator? Try just using the Terminal to see if that makes a difference. Also, are these files in a cloud service, or local? I wonder if that is affecting it for you.
    As for videos, that's different. Most of the videos I see don't even have a date in the metadata. No idea what to do there to get what you want, sorry.

    Elissa
    3 years ago

    I'm using the automator-made-app! The files are local, in finder in a folder. Before, (within the last 2 years), if I airdropped photos/videos from my iphone, the date was capture time/date so when I moved them to folders it was neatly organized by date. Now they're all under the date that I airdrop them so everything is a mess... You have been an absolute lifesaver and I am so grateful for you and this site.

    Jason
    3 years ago

    Great info, thanks Gary! Can this be adapted so that we use the original date/time to append the filename of the file instead of changing the file create date? e.g. if I have DSC_0143.NEF photographed Jul 20, 2008 at 3:59:33 PM (PDT, so -7) but the file create date is wrong, as long as I can rename this file to "2008-07-20 1559 DSC_0143.NEF" then I care less what the actual Created Date time is.

    3 years ago

    Jason: Possibly. You'll have to play around with it and see what you can do. I don't know offhand.

    Fabrice
    3 years ago

    Nice tutorial, but problem is on Photos that uses date file instead of exif dates to sort pictures.
    Each time I modify a picture with a third app, date in exif is still correct (eg. in 1984) but file date change to the current date (eg. in 2022). Then in Apple Photos this pictures is displayed in year 2022 instead of 1984.
    So using this script would be nice, but recently I change my iPhone, each time I plug it on my mac photos are duplicated... I don't know why ! (icloud ?)
    Worst Apple App !

    3 years ago

    Fabrice: If you want to change the date in the Photos app, you can do that easily in the Image menu. If you are using iCloud Photos you should never import your photos when you connect your iPhone to your Mac. Just turn that option off as there is no need to sync your photos AND use iCloud at the same time.

    Todd
    3 years ago

    Hi Gary. Thank you for this wonderful tutorial. I have been trying to create this Automator Application to work on videos using the ExifTool. So far I came up with this:

    [CODE]touch -t `exiftool -CreateDate -d "%Y%m%d%H%M.%S" -s3 "/path/to/file.mp4"` "/path/to/file.mp4"[/CODE]

    It works when typed into the terminal but I can't figure out how write the Shell Script for the Automator Application. Im mostly not sure how to deal with the file path. Do you think you can help please. Thank you.

    3 years ago

    Todd: The example in this video uses a parameter for that. You see that passed in as $@ and then the loop will assign each file to $f. Try to fully understand how this works in my example before trying to apply it to a different shell command. Perhaps team up with a colleague why knows shell scripts if you don't have the experience yet.

    Dermott D
    3 years ago

    hi is there a possibility of using kMDItemFSName as the file creation date, as this is the correct creation date but with transferring files back and forth from a few different drives the creation date shows the date I transferred them to the computer.

    thank you

    3 years ago

    Dermott: Do you mean set the name of the file to include the date? I suppose you could do that if you wanted. I regularly name my tutorial projects with the date in the name.

    Dan
    3 years ago

    Hi
    I have found a better solution for setting the created date as the same as modified date. Your method doesn't work for me when the files got a created date 2000-01-01, which it will become after backing up from iPhone to my server with a dedicated app from the manufacturer.

    The command SetFile seems to be a better option on all levels, and you don't even have to think about the hour adjustment.

    for f in "$@"
    do
    SetFile -d "$(GetFileInfo -m $f)" $f
    done

    3 years ago

    Dan: That's a good solution but it only works if you have Xcode and the Xcode command line tools installed. Most people don't.

Comments are closed for this post.