Fix file timestamps with dates in the future on OS X or Unix/Linux

I recently needed to fix a heap of files with invalid timestamps for a client to sync between two sites.

The issue was that some files had modification dates in the year 2032 or something like that. With a 2-way sync, if one file changes the modification date is used to determine which one wins. If the unchanged file has it’s date set in the future, then it could overwrite the changes made that set the other file to the current date and time.

Fortunately fixing this is really easy:

touch /tmp/now
find -xdev /Path/To/Server/Share\ Point -newer /tmp/now -print0 | xargs -0 touch -t 201610131030
rm /tmp/now

I specifically had to set the date and time to an exact date and time (-t 201610131030) rather than letting touch set the files to the current date and time so that they didn’t have time stamps off by a few seconds at each end, causing them to be resynchronised.

The number given to touch is of the format YYYY MM DD HH MM SS. The timestamp is in the local time zone, so I also had to adjust the timestamp given to the files on the remote server as it’s in a different time zone.

This article was posted by Kai Howells. If you liked this content and have any technical work in the Melbourne area, say hello via my contact form or give me a call on 0419 361 653 - I cover most of the greater Melbourne area and my rates are competitive.

Leave a Reply

Your email address will not be published. Required fields are marked *