which is the rsync command to «smartly» merge two folders?

42

26

I have some folders that got duplicated by mistake and I have to merge them together. Let's say folder A contain file X and folder B contains file Y, I would like to get file X copied to folder B. But, of course, I can face more complex cases, where f.i. both folder A and B contain the same file X: in those cases, I would like to keep the newest copy of the file, so rsync should compare both files, and if the origin is older just skip, it otherwise the destination is the older one, overwrite.

Which parameters should I put on the rsync command line?

This is what I found googling, but I do not think is correct.

rsync -avz --ignore-existing src dest

The man page is somehow beyond me.

Thank you.

PS in those cases where a file exists in both folders, the better thing would be to overwrite the older one into the destination folder and at the same time keep a copy of the older, just as a backup in case you should get back to the previous

Tiziano Solignani

Posted 2013-02-06T08:41:04.397

Reputation: 423

Answers

70

I would use:

rsync -abviuzP src/ dest/
  • -i turns on the itemized format, which shows more information than the default format
  • -b makes rsync backup files that exist in both folders, appending ~ to the old file. You can control this suffix with --suffix .suf
  • -u makes rsync transfer skip files which are newer in dest than in src
  • -z turns on compression, which is useful when transferring easily-compressible files over slow links
  • -P turns on --partial and --progress
  • --partial makes rsync keep partially transferred files if the transfer is interrupted
  • --progress shows a progress bar for each transfer, useful if you transfer big files

user49740

Posted 2013-02-06T08:41:04.397

Reputation: 2 850

2Really nice, I ended up using rsync -abuP – qed – 2013-07-23T09:24:43.127