cp -l hard links files instead of copying them, saving filesystem space. I need to use rsync instead of cp because of its --exclude capabilities.

So my question is, how do I get rsync to hard link files instead of copying them? Obviously, this is a local filesystem copy. I've read the docs for rsync's -H option, but it's unclear to me if this behaves the same way as cp -l.

  • It looks like I can get this behavior with --link-dest=$SOURCE_DIR: slaptijack.com/system-administration/… I'm testing this out now. – nnyby Sep 22 '18 at 18:33
  • Generally, rsync is used for back-up or archive purposes. Why would you want a back-up of file names without their contents? – AFH Sep 22 '18 at 18:37
  • @AFH my backup script creates rolling backups of gigabytes of data at many points in time, but most of this data is the same. I save disk space by copying these between time points with cp -al. Without using hardlinks, all these different copies of data (if they were real files) wouldn't fit on my 2TB drives. See: slaptijack.com/system-administration/… – nnyby Sep 22 '18 at 18:42
  • So you can recover from accidental deletions, thought not overwrites, but you'll never be able to use your back-up to recover from a disc failure or partition loss. I use cp -au, which copies only changed files, so the process doesn't take too long, but it does give me an external copy of the latest version of every file for recovery purposes. Of course, to back up Gigabytes of data you need Gigabytes of storage, but if your files are important you need to make that investment. Only unimportant files are not backed up, by definition. – AFH Sep 22 '18 at 19:02

The answer has turned out to be yes, with rsync's --link-dest option. It's just not obvious because it's not a simple on/off flag like cp -l, because it takes a path argument.

So, cp -l a/ b/ can also be done like this:

rsync -a --link-dest=a a/ b/

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.