27

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?

2
  • When do you lose timestamps?
    – lhf
    Commented Jan 31, 2013 at 11:06
  • Copying them how?
    – Gerry
    Commented Jan 31, 2013 at 11:20

5 Answers 5

20

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
3
  • +1 A simple cp -p is straight-forward, preserves the modified time (likely the most useful of the three timestamps on a file) and doesn't require creation of an intermediate archive file.
    – wberry
    Commented Jan 28, 2014 at 19:57
  • 1
    As far as I know, "cp -p" forgets the creation date and instead sets it to the modif date. Which is quite an absurd behaviour. Commented Sep 3, 2014 at 9:19
  • 1
    cp -p doesn't work when copying from HFS+ to FAT32 though. Commented Mar 6, 2016 at 21:49
11

I use rsync to do this sort of copy. However note the version supplied by Apple is 2.6.9 and has bugs on this. So you need to get a third party built one either build yourself or via a package manager

for example

rsync -aEAX source_dir target_dir

The option -E copies the ACLs and -a preserves the unix permissions and times A copies the ACLs and X copies the xattributes (onm rsyn version 3. Version 2 does not have AX)

rsync can also be setup to copy to remote machines without mounting drives.

There are some GUI front ends for rsync e.g. aRsync. For other directory synchronization tools see this question.

9
  • rsync is a good solution, but be aware that it doesn't preserve access times. pax (see my answer below) does.
    – jaume
    Commented Feb 18, 2013 at 10:26
  • 1
    As far as I know, "rsync" forgets the creation date and instead sets it to the modif date. Which is a big failure. Commented Sep 3, 2014 at 9:20
  • → jaume: any command making any form of copy of "file a" toward "file b" has to read "file a" first. Hence the access time of "file a" should be modified. This isn't a feature at the command level but at the kernel level. If a command, through another system call, restores the access time to its previous state, then this is a dangerous trick, because it is a plain lie.
    – athena
    Commented Sep 3, 2014 at 10:15
  • → Nicolas: cf. man rsync and search for preserve times :) ⇒ the answer from Mark is correct.
    – athena
    Commented Sep 3, 2014 at 10:18
  • 1
    → Mark, @daniel - I have just tested rsync in Mac OS X 10.6.8. A local copy of a folder with "rsync -rt" forgets the creation date of files and replaces it with their modif date. Despite the manual for "rsync" saying "preserve times" for the option -t. The bug is probably present in Mavericks and Yosemite too. Commented Sep 10, 2014 at 16:48
4

Use pax. The default pax format, called ustar, preserves file modification and access times (among other things like user ID, group ID, file mode bits and extended attributes like Spotlight comments and ACLs). See pax man page here for more details.

First, create a pax archive on every Mac and copy it to the external hard drive like this:

  1. Open Applications>Utilities>Terminal.
  2. Type in Terminal:

    $ cd
    

    and drag the folder where the files to be combined reside on that Mac to Terminal:

    enter image description here

    Alternatively you can type the full folder name:

    $ cd /path/to/your\ folder
    

    This will change the current folder to 'your folder'.

  3. Archive the folder with pax:

    $ cd ..
    $ pax -w "your folder" > yourfolder.ustar
    
  4. Use the Finder to copy the newly create archive yourfolder.ustar to the external hard drive.

Then extract the archives with pax:

  1. Open Terminal on the Mac that has the external USB hard drive plugged in.

  2. Change the current folder to the single folder hierarchy on the external hard drive with command cd as explained above:

    $ cd /Volumes/externalHDD/path/to/single \folder
    $ ls
    yourfolder.ustar
    yourfolder2.ustar
    yourfolder3.ustar
    
  3. Extract the archives:

    $ pax -r -p e < yourfolder.ustar
    $ pax -r -p e < yourfolder2.ustar
    $ pax -r -p e < yourfolder3.ustar
    $ ls
    your folder
    your folder 2
    your folder 3
    
  4. Move the files around with the Finder if you need to (the Finder preserves file modification and access times within the same volume).

(I've tested this procedure on OS X 10.8 (Mountain Lion).)

1
2

Compress them with zip or select them with ctrl-click or right-click and choose Compress. Copy the compressed files to your target system and open them there.

If you can, format your external drive as a Mac OS filesystem, not FAT.

2
  • Not Option-click. Ctrl-click. Or right-click. Commented Sep 3, 2014 at 9:25
  • 1
    The best choice for formatting the disk is Mac OS Extended Journalled. Mac OS Extended is also called HFS+. Commented Sep 3, 2014 at 9:27
0

I tried out what jaume suggested above (thanks jaume!), but for me (on my very old hardware) creating .ustar files was too time consuming and I ran out of patience...hours later.

So I read the examples here, and adapted an example like so:

pax -r -u -w [source directory] [destination directory]

In my case I changed directory with the 'cd' command and checked terminal could see my source directory 'User' with the 'ls' command before running:

pax -r -u -w User /Volumes/T7

The results were that all files maintained their original date modified/created - but directories had 'today' as their date modified date & original date for 'date created'.

This was good enough for me!

P.S. I'm not sure if pax is built into Darwin as I installed TigerBrew before getting started but I did all of this on OS X 10.7.5.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .