MacOS – Can timestamps be preserved when copying files on OS X

file-transfermacos

I'm interested in combining the files from three Macs by copying the files from each one into a single folder hierarchy of my design on an external hard drive.

In my past experience copying files in OSX, sometimes the files lose their Created and Modified timestamps; i.e. they are changed to the present moment when the file copy happens.

How can I make sure this doesn't happen?

Best Answer

I just ran across this myself, and the built in cp command actually handles it.

I discovered a bunch of old CF cards that I wanted to harvest the pictures from. My processing scripts will look at the file mtime to put it in the correct place so I needed it preserved.

From the man page:

 -p    Cause cp to preserve the following attributes of each source file in the copy: modification time,
       access time, file flags, file mode, user ID, and group ID, as allowed by permissions.  Access
       Control Lists (ACLs) and Extended Attributes (EAs), including resource forks, will also be pre-
       served.

       If the user ID and group ID cannot be preserved, no error message is displayed and the exit value
       is not altered.

       If the source file has its set-user-ID bit on and the user ID cannot be preserved, the set-user-
       ID bit is not preserved in the copy's permissions.  If the source file has its set-group-ID bit
       on and the group ID cannot be preserved, the set-group-ID bit is not preserved in the copy's per-
       missions.  If the source file has both its set-user-ID and set-group-ID bits on, and either the
       user ID or group ID cannot be preserved, neither the set-user-ID nor set-group-ID bits are pre-
       served in the copy's permissions.

So, using zsh I was able to run (NO NAME being my cards volume name):

cp -rvp /Volumes/NO\ NAME/DCIM/**/*.{JPG,jpg} ~/Desktop/tmp/pics

I believe that the special /**/* construct is specific to ZSH; however you could do something like

find /Volumes/WHATEVER -type d -print0 | xargs cp -vp {}/*.JPG /my/out/path