How can I set the time information of a file to be the same as of another file on OSX / Linux?
What's an elegant way to copy the creation and modification dates of a file to another file?
touch -r FROMFILE TOFILE
That sets the access and modification times of TOFILE to match FROMFILE.
It will also update TOFILE's ctime to the current time.
There's no way to update the ctime (inode change time) to match another file, only to the current system time. (Which you can change if you really need to set the ctime, or use
debugfs on an unmounted ext2/3/4 filesystem. See more on Stack Overflow)
13And note: the infamous "creation time" (aka "ctime") that might hear people say that UNIX systems store for a file is really the "i-node modification time", which virtually useless, and certainly doesn't represent the time the file was created.– FranJun 16, 2012 at 15:47
7The real "creation time" is btime aka "birth time", which some filesystems support but Linux stat() doesn't (yet).– user1686Jun 16, 2012 at 16:07
touch -ron OSX, the "creation time" (as appears in the "get info" box for a file) becomes the same as "last modified" time. i.e. it seems that the FILE1 creation time gets ignored/lost in the process– GJ.Jun 17, 2012 at 11:38
2@Fran I wasn't aware of how ctime works. I found a good explanation of it here.
http://www.unix.com/tips-tutorials/20526-mtime-ctime-atime.html– JoeJun 18, 2012 at 19:58
1In addition to the above answer in case someone needs to copy original timestamps from multiple files to another set of multiple modified files with the same name here is a possible solution:
for i in $(ls old/);do touch -r "old/$i" "new/$i";done(this assumes you have original files in "old" directory and new files in "new" directory. Sep 4, 2020 at 10:56