I'm trying to copy a directory tree recursively preserving hardlinks to the file. Using gnu cp, this would work with the -l flag. cp would then recreate the directory structure, but wouldn't need to copy the contents of each file.

This is preliminary to a backup, first I want to make a cheap (hardlinked) copy of the previous backup and then rsync the source directory over this copy. Roughly:

 cp -r -l yesterdays_backup todays_backup
 rsync -a source_dir todays_backup

Unfortunately, OSX's cp doesn't support the -l flag, as far as I can tell, cpio doesn't support recursive copying. The other alternative is pax, but that leads to the entire directory structure being copied:

 pax -rw backups/yesterdays_backup backups/todays_backup


  \source_dir (...)



There should be an easy/obvious way to do this, but I'm currently stumped... Any alternatives to cpio and pax? I'd like to avoid having to install gnu cp.

I'm aware of Timemachine, but that won't properly back up encrypted directories incrementally.

migrated from stackoverflow.com Sep 1 '09 at 0:48

This question came from our site for professional and enthusiast programmers.


It is easy enough to install cp from MacPorts, however, if you don't want to, or want to create a portable script, then you have three options:


rsync --archive --link-dest=../yesterdays_backup backups/yesterdays_backup\


mkdir backups/todays_backup
cd backups/yesterdays_backup
find . -print | cpio -p -al ../todays_backup


mkdir backups/todays_backup
cd backups/yesterdays_backup
pax -rwl . ../todays_backup

It's easy to install the coreutils package from MacPorts which contains the GNU cp command renamed to gcp.

But even better, newer versions of rsync, including the one in OS X 10.5 at least, support the --link-dest option which should eliminate the need for the initial cp -al. See here. It's good practice to use the -E option, too, to copy extended attributes, ACLs, etc.


Although cpio does not support recursive copying directly, you can feed the exact list of files you want to copy into its standard input, and achieve the effect of a recursive copy using find piped into cpio. This is directly analogous to using GNU tar with the options -T - (which means read the list of files to back up from standard input).


I believe what you want can also be achieved with ditto yesterdays_backup todays_backup. By default, ditto does a recursive copy that preserves hard links, ACLs, and extended attributes.

  • the problem is that the copy is not cheap any more when the objective is to create hard links across directory scope. ditto can preserve them when they exist, but it can't be told to link instead of creating a duplicate. – Florenz Kley Apr 20 '11 at 21:49
  • I don't think ditto can even be told to preserve directory hard links. man page says it only does file hard links – Chris F Carroll Oct 3 '17 at 17:36

+1 for Pauls's answer. ditto would be an option, but only if used in conjunction with find - it does preserve hard links, but only inside directories.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy