Carrione & Swift

Not just iOS development articles, tips and useful resources.

Share on Twitter

How to change a file’s last modified and creation dates on macOS

Published on 8 Dec 2020

The finder only can read file metadata about creation and modification dates. Sometimes we can be in a situation where we need to change them. How can we do it? We can use the touch command in a terminal.

The touch command sets the modification and access times of files. If any file does not exist, it is created with default permissions. In some cases, it can change the creation date.

So open the terminal and write this:

$ touch -t 202012080000 /path/to/your/file

This will change the file modification and access dates to December 8, 2020, at 12:00 AM. And if the file had a creation date higher than the newly inserted date, then the file creation date is also changed.

Date format

We need to use dates in this format: CCYYMMDDhhmm.SS

CC - (optional) The first two digits of the year (the century). This is an optional value with a default value equal to the current century.

YY - (optional) The second two digits of the year. If "YY" is specified, but "CC" is not, a value for "YY" between 69 and 99 results in a "CC" value of 19. Otherwise, a "CC" value of 20 is used. This is an optional value with a default value equal to the current century.

MM - The month of the year, from 01 to 12.

DD - the day of the month, from 01 to 31.

hh - The hour of the day, from 00 to 23.

mm - The minute of the hour, from 00 to 59.

SS - (optional) The second of the minute, from 00 to 61. This is an optional value with a default value of 0.

Example:

We have a file with the name "yourFile", its path is /Users/You/Work/yourFile and its creation date is December 7, 2019, at 12:00 AM. Here are its details:

File Name                       : yourFile
File Modification Date/Time     : 2019:12:07 00:00:00+01:00
File Access Date/Time           : 2019:12:07 00:00:00+01:00
File Creation Date/Time         : 2019:12:07 00:00:00+01:00

We will change its modification and access times to December 7, 2020, at 2:30 PM. Write this in your terminal:

$ touch -t 202012071430 /Users/You/Work/yourFile

Now the file details are changed to this:

File Name                       : yourFile
File Modification Date/Time     : 2020:12:07 14:30:00+01:00
File Access Date/Time           : 2020:12:07 14:30:00+01:00
File Creation Date/Time         : 2019:12:07 00:00:00+01:00

Note that only the access and modification dates are changed. The creation date is not changed. Now we will change the modification and access times to December 7, 2011, at 9:00 AM. This is the lower date than the current creation date.

$ touch -t 201112070900 /Users/You/Work/yourFile

Now the file details are changed to this:

File Name                       : yourFile
File Modification Date/Time     : 2011:12:07 09:00:00+01:00
File Access Date/Time           : 2011:12:07 09:00:00+01:00
File Creation Date/Time         : 2011:12:07 09:00:00+01:00

Note that we changed the modification, access, and even creation date.

You don't need to write the file path!

I know it's annoying to always type the file path. Fortunately, you don't have to do that. You can drag the file path from the finder.