I posted the below question to stackoverflow, but people suggest I posted here instead:

I have two directories, dirA and dirB. I want to move everything in dirB that are different from dirA (checksum different, not mod-times) to a new directory dirC.

I am looking at rsync -c command, but it seems like rsync can only copy from one directory to the other, instead of compare and then copy to a new directory.

I can also write a long script that uses diff -qr to get the file names and then analyze the output and copy out the files accordingly, but I really want to avoid parsing anything (that's where things need testing and debugging).

How can that be done reliably?

You will have to use the --compare-dest option. From the Manual:

--compare-dest=DIR:

This option instructs rsync to use DIR on the destination machine as an additional hierarchy to compare destination files against doing transfers (if the files are missing in the destination directory). If a file is found in DIR that is identical to the sender's file, the file will NOT be transferred to the destination directory. This is useful for creating a sparse backup of just files that have changed from an earlier backup.

Your command will be something like:

rsync -aHxvc --progress  --compare-dest=dirA/ dirB/ dirC/ 

(I have chosen for you common flags aHvx, please check that these suit your needs). Before running this program it is best to run the command

rsync -aHxvc --dry-run --progress  --compare-dest=dirA/ dirB/ dirC/ 

which will show you the operations which will be carried out, without actually performing them: it gives you an opportunity to control that the command really does what you mean it to do.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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