Changing Creation Date of a file

Solution 1:

touch -t normally only changes the modification and access times. It only changes the creation time if the target time is before the original creation time.

touch -t 199912312359 file.txt
touch -t $(date -jf %FT%T 1999-12-31T23:59:59 +%Y%m%d%H%M%S) file.txt

SetFile -d always changes the creation time.

SetFile -d '12/31/1999 23:59:59' file.txt
SetFile -d "$(GetFileInfo -m test.txt)" file.txt

SetFile is part of the command line tools package which can be downloaded from developer.apple.com/downloads or from Xcode's preferences.

Solution 2:

In Mac OS X there appears to be four different timestamps associated with a file. These timestamps are not always updated independently from each other, but one can have some individual control with a bit of care and with use of touch, SetFile, and chown (or chmod). To see this, let's experiment and create a new file from the command prompt #:

# touch a.txt
# stat a.txt
16777218 5969798 -rw-r--r-- 1 username staff 0 0 "May 19 00:11:14 2015" "May 19 00:11:14 2015" "May 19 00:11:14 2015" "May 19 00:11:14 2015" 4096 0 0 a.txt

The first date listed is last access time (atime), the second is last modification time (mtime), the third is last status change (ctime), the fourth is time of file creation (birth, Btime), see man lstat.

To modify access time (atime) to a specified time use:

# touch -a -t201412312359.59 a.txt
# stat a.txt
16777218 5969798 -rw-r--r-- 1 username staff 0 0 "Dec 31 23:59:59 2014" "May 19 00:11:14 2015" "May 19 00:11:46 2015" "May 19 00:11:14 2015" 4096 0 0 a.txt

To modify modification time (mtime) and time of file creation (Btime) to a specified time use:

# touch -m -t201312312359.59 a.txt
# stat a.txt
16777218 5969798 -rw-r--r-- 1 username staff 0 0 "Dec 31 23:59:59 2014" "Dec 31 23:59:59 2013" "May 19 00:12:19 2015" "Dec 31 23:59:59 2013" 4096 0 0 a.txt

Apparently this also automatically updates the time of last status change (ctime) to the current system time!

To modify only the time of creation/birth (Btime) to a given time, use (as proposed by others above):

# SetFile -d "12/31/1999 23:59:59" a.txt
# stat a.txt
16777218 5969798 -rw-r--r-- 1 username staff 0 0 "Dec 31 23:59:59 2014" "Dec 31 23:59:59 2013" "May 19 00:12:19 2015" "Dec 31 23:59:59 1999" 4096 0 0 a.txt

To modify only the time of last status change (ctime) to the current system time use for example:

# chown username a.txt
# stat a.txt
16777218 5969798 -rw-r--r-- 1 username staff 0 0 "Dec 31 23:59:59 2014" "Dec 31 23:59:59 2013" "May 19 00:13:45 2015" "Dec 31 23:59:59 1999" 4096 0 0 a.txt

where username is the owner of the file (as reported by stat or ls -l). This doesn't change the ownership of the file, but accesses the file's inode and records this as a time of last status change. Unfortunately, it is not possible to specify the time explicitly here other than by changing the computer's time...

In summary, to be able to change the four timestamps independently, one may use (in this order for the last 3):

  1. touch -a -tCCYYMMDDHH.SS <file>
  2. touch -m -tCCYYMMDDHH.SS <file>
  3. SetFile -d "MM/DD/CCYY HH:MM:SS" <file>
  4. [set system date] chown username <file> [revert to normal system date]

(Of course, resetting the system date may not be a risk-free operation, you've been warned.)

Solution 3:

The command-line touch command works in Lion, but you need a little background on how to use it.

Firstly, you need to type it in, it's not a utility that operates graphically in a window. The place you type it in is an app called Terminal. You can find this in your Utilities folder, or use Spotlight search to find it.

Once you start it, you get a command line window where you can type in commands. Just because you are typing doesn't mean it has anything to do with coding, but to someone new to "old fashioned" OS commands it can look a little intimidating.

So, the command is called touch, and after the command you can pass various parameters, the only required one being the filename. Typing the following will set the last modified date of the file to the current time:

touch filename

Of course, you may need to find the file first, which could require further commands to get to it. the cd or Change Directory command helps here:

cd ~/Desktop/files/PDFs
touch filename

Or, you can simply add the full path to the file as part of the command:

touch ~/Desktop/files/PDFs/filename

More advanced usage of the touch command allows you to specify the time you want to set, rather than using the time at execution, set access instead of modification times etc. This is what parameters like -t and others do. Some set the time to be the time of another file, some set it to go back, or forward in time by X amount, (see here for more details), eg:

touch -d '1 May 2005 10:22' file8

The following is from this page which is also useful reference, and a great example with some tips for those who are uncomfortable with file locations on the command line

Note that this will change both the date modified and date created attributes only if your file was created after the date you enter. This is because a file cannot be modified before it has been created. If created before the date you set, only the modified date will be changed.

How to change the date created attribute of a file?

  1. Open up the Terminal application (/Applications/Utilities/Terminal.app) or if you prefer use iTerm.

  2. Type this into Terminal (without hitting enter/return) inserting the date in the format of YYYYMMDDhhmm:

    touch -t 201212211111
    
  3. Open a Finder window and locate the file you wish to modify and drag and drop it into the Terminal window, it will then insert the file location using command line terminology. Here’s an example of what should be typed into the Terminal at this point:

    touch -t 201212211111 /Volumes/Mac\ HD/Pictures/somefile.jpg
    

    201212211111 in the example above represent “December 21, 2012 11:11 AM