Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I have a script that converts a file to another format, and I want the resulting file to have the same timestamps as the original, including the creation time (as shown by ls -lU).

I can use touch to set the access and modification time, but I don't see a way to set the creation time.

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted
touch -t YYYYMMDDhhmm file.ext

This will set the modification and creation dates of a file. I ran it on a file I created this morning, and here's what Get Info says:

enter image description here

share|improve this answer
    
I should of course have tested this first :-) Thanks –  dkagedal Jul 1 '11 at 19:59
    
Nathan, did the file exist before you "touched" it? touch doesn't change creation dates unless it creates the file itself. –  patrix Aug 2 '12 at 13:51
    
-1 touch will only set the creation date of a file if it creates the file, or if the new date is earlier than the file's creation date. (It's impossible to have a file whose modification date is earlier than its creation date.) So this solution does not work if the new date is later than the file's creation date. The SetFile solution will always work. –  TachyonVortex Aug 21 '13 at 2:13
add comment

SetFile can also be used to set the creation date before the modification date or in the future.

-d date    Sets the creation date, where date is a string of the
           form: "mm/dd/[yy]yy [hh:mm:[:ss] [AM | PM]]" Notes:
           Enclose the string in quotation marks if it contains spa-
           ces. The date must be in the Unix epoch, that is, between
           1/1/1970 and 1/18/2038. If the year is provided as a two-
           digit year, it is assumed to be in the 21st century and
           must be from 00 (2000) through 38 (2038).

This would set the creation date to the modification date:

SetFile -d "$(GetFileInfo -m test.txt)" test.txt

SetFile and GetFileInfo are part of the command line tools package, which can be downloaded from developer.apple.com/downloads or from Xcode's preferences after installing it from the App Store.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.