How to set the creation date of a file in Mac OSX to the current date?

Viewed 565

I have tried to do this in terminal:

touch -t `date +%yy%mm%dd%HH%MM`/path/to/file

but it gives the error touch: out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]. How can I get the right date currently?

1 Answers

Try running date +%yy%mm%dd%HH%MM by itself, and the problem should become apparent:

$ date +%yy%mm%dd%HH%MM
19y10m26d11H40M

The format code for a two-digit year is %y, so %yy gives a two-digit year followed by a stray "y". All the others work the same way. So just un-double all the format codes. Oh, and I recommend using $( ) instead of backticks:

$ date +%y%m%d%H%M
1910261141
$ touch -t $(date +%y%m%d%H%M) /path/to/file

But it's actually easier than that, because touch defaults to the current time. So all you actually need is touch /path/to/file.

Related