"Touch" won't change file creation date -why?

Discussion in 'Mac Programming' started by macstatic, Apr 23, 2016.

  1. macstatic macrumors 65816


    Oct 21, 2005
    I'm trying to find a tool which will allow me to change a file's creation date ansd from what I've read, using the "touch" terminal command you can change both creation and modification dates as long as it's changed to something earlier than the current modification date, but I haven't gotten this to work. In theory the following two steps should handle it (with 2012-09-14, 10.01 am as the creation/modification dates):

    $ touch FILENAME.PNG
    $ touch -t 201209141001 FILENAME.PNG

    ... but no matter what I do it only changes the modification date. From what I've read it used to be possible to change the creation date without any restriction, but with OSX 10.9 Mavericks the above restriction should apply. Have I misunderstood, and it's actually no longer possible to change the creation date with "touch"? I'm on OSX 10.9.5.
  2. chown33 macrumors 604

    Aug 9, 2009
  3. macstatic, Apr 24, 2016
    Last edited: Apr 24, 2016

    macstatic thread starter macrumors 65816


    Oct 21, 2005
    Thanks. Setfile is an option, but one I've put aside since it involves signing in and downloading the huge Xcode package, but reconsidering it after reading your reply I found info on how to install the OSX command line tools without installing all of Xcode. In short, just enter the following terminal command:

    $ xcode-select --install

    By the way, does anyone know how to download the above and keep the installation package?
    UPDATE: having read this I found out I could sign in with my Apple ID to 'downloads for Apple developers' where I found Command line tools for OSX 10.9 for Xcode 6.1.dmg (if I'm not mistaken that's the latest one for OSX 10.9 Mavericks -please correct me if I'm wrong).

    Anyway, I tried setfile and indeed, it does allow changing the OSX file creation date (and unlike touch (which changes both creation and modification dates at once) it allows changing only the creation date). For instance to February 28th 2016, 13:21:59 (1:21:59 pm):

    $ setfile -d "02/28/2016 13:21:59" FILENAME.PNG

    The downside is the rather strange date format which I'm guessing would take some clever programming to work with other tools.

    As for touch, I don't know what I did wrong -I must have missed a digit or two when I tried it out. The following example works fine, which sets both creation and modification dates to January 31st 2016, 22:14:33 (10:14:33 pm):

    $ touch -t "201501312214.33" FILENAME.PNG

    It only allows the creation date to be changed to something earlier than the current modification date. If that's the case then both creation and modification dates are changed to that date.
    If the entered date is later than the current modification date, then only the modification date is changed.

    Actually, despite the above limitations this works fine in my case (now that I've gotten touch to work!) as I would only need to change the dates to something earlier than the modification date anyway, but it's nice to see I have options ;)
    In any case (writing the creation date from image files' EXIF tags) I need to create an Automator script which reads the [XMP-exif]DateTimeOriginal (EXIF) tag and write that date/time information (using touch or setfile) to the file's creation date. I'm guessing I would have EXIFtool read the DateTimeOriginal EXIF tag, output it as a variable which would be passed on to the touch or setfile command. Being a novice in the programming department, how would I actually do this?
    --- Post Merged, Apr 24, 2016 ---
    Much appreciated! I didn't know about that site which could be useful for other stuff as well (hoping it would answer my question above about how do download the Xcode command line tools on their own, but it appears not to contain them).

    Interesting thought. Are you saying I could somehow create an Automator script and create a self-contained app with (that older version of) the touch command? That would certainly make it a lot easier to share with others.
  4. CarlJ macrumors 68020


    Feb 23, 2004
    San Diego, CA, USA
    BTW, an obvious solution this suggests is, touch the file twice, first to set the time to now (or some point in the future), then a second time to set the creation and modification times to whatever you want - since you'll be setting a time before now (and thus before the time on the file), you'll definitely get them changed.

    FWIW, touch likely isn't doing anything special with the file creation time. Unix traditionally has three timestamps associated with a file: atime (access: last file read), mtime (modification: last file write), and ctime (change: last modification to the inode). Touch has traditionally allowed modification of mtime and optionally atime, while ctime is entirely under the system's control, for purposes of security and system integrity - you could read a file then reset its atime, but that will result in the ctime changing. (Many folks have fallen into the trap of assuming ctime is "creation time" - you're clearly not making that mistake). The creation timestamp is something outside traditional Unix purview, presumably is available on files in Mac OS X because it is present in the HFS+ filesystem. I'm guessing the kernel is making the decision to change the file creation time when instructed to change the mtime (wouldn't make sense to have a file that was modified before it was created).

    Finally, once you have access to SetFile, you also have access to GetFileInfo. Worth knowing about.
  5. macstatic thread starter macrumors 65816


    Oct 21, 2005
    Nice tip about touching the file twice or setting it to a future date first to bypass its limitations!

    I've never quite understood why those file dates don't have anything to do with OSX, but your explanation about it being under the control of HFS+ makes perfect sense.

    Setfile is an option of course, but the date format is even stranger than that of touch, so I'm a bit concerned about that being compatible with the output of exiftool.
  6. macstatic, Apr 30, 2016
    Last edited: May 1, 2016

    macstatic thread starter macrumors 65816


    Oct 21, 2005
    It works!
    The following command-line reads the "DateTimeOriginal" EXIF tag from the file then uses that information to write the same file's creation and modification date:
    touch -t `exiftool -s -s -s -d "%Y%m%d%H%M.%S" -DateTimeOriginal FILENAME.JPG` FILENAME.JPG
    The next part is to create an Automator script which allows me to drag & drop one or several image files (or a folder with images within) and process them using the above command. Based on help I've gotten earlier with an Automator script I'm hoping the following will work as a "run shell script" Automator action?:

    for f in "$@"
    touch -t `exiftool -s -s -s -d "%Y%m%d%H%M.%S" -DateTimeOriginal "$f"` "$f"
    Furthermore, to refine the script it should ideally first check a few things prior to actually proceeding to touch the file:

    1) is it a suitable file type (JPG, PNG, PSD or TIF)?
    • if YES; move to step 2
    • if NO; skip the file and move on to the next file (repeat step 1 with the next file)
    The following seems to determine the filetype (the command result being: PNG):
    $ exiftool -s -s -s -tag "-Filetype" TESTFILE.PNG
    2) is a "DateTimeOriginal" EXIF date tag found within the file?
    • if YES; move on to step 3
    • if NO; skip the file and move on to the next one (go to step 1 with the next file)
    I'm not sure how to make it check (with a no/yes response) if it's found, but the following command analyzes the file and gives all the relevant info in one go. I'll continue to look into that.
    exiftool -a -G1 -s TESTFILE.PNG
    3) Is the date and time in "DateTimeOriginal" identical to that of the file's creation date?
    • if YES; skip this file and move on to the next one
    • if NO; proceed to touch the file (as in the command-line above) -go to step 1 with the next file
    Again it seems Exiftool can analyze the file (here reformatted to the same format touch uses, though I'm not sure if that's needed or not):
    exiftool -s -s -s -d "%Y%m%d%H%M.%S" -tag -DateTimeOriginal TESTFILE.PNG
    How would I go about adding these conditions to my Automator script?

Share This Page