I want to use rsync to compare two directories and its file contents and transfer it to a new empty directory. So i have created a small POC:




The main.js file in NEW and OLD are identical and have the same checksum. However when I use:

rsync -rvcm --compare-dest=OLD/ NEW/ CHANGES/

the main.js file still gets copied to the CHANGES folder. How can I ensure it doesn't do that? I only want to copy the files, which have a different checksum.




If DIR is a relative path, it is relative to the destination directory. […]

(source: man 1 rsync)

In your command

rsync -rvcm --compare-dest=OLD/ NEW/ CHANGES/

OLD/ is a relative path. The destination directory is CHANGES/. OLD/ being "relative to the destination directory" means it's in fact CHANGES/OLD/ with respect to the current working directory. This directory most likely does not exist in your setup. Even if it exists, it's not the directory you mean anyway.

You mean OLD/ in the current working directory, which can be referred to as CHANGES/../OLD/. The part relative to CHANGES/ is ../OLD/, therefore:

rsync -rvcm --compare-dest=../OLD/ NEW/ CHANGES/

Note if CHANGES is a symbolic link to a directory then CHANGES/../ is not necessarily ./ (compare Why does ls .. show real parent content when I'm inside a symbolic link directory?). In this case CHANGES/../OLD/ does not mean OLD/. Build the --compare-dest=… argument according to the physical directory structure.


Generally, it's better to include timestamps and stop mandating a checksum

rsync -rtvm --compare-dest=OLD/ NEW/ CHANGES/

If you are copying between Linux-native filesystems you may want to use -a instead of -rt.

Since you're using local copies (NEW/ and CHANGES/ appear to be on the same system) there's no point in verifying with a checksum - it's going to be much the same speed simply to copy the file anyway.

  • Well the thing is, I want a mechanism to compare two folders and its file contents and only copy the different files to the "CHANGES" directory. That's why I guess I need the checksum verification, because in this PoC the main.js shouldn't be copied, since the content/checksum is the same. Jun 7 at 11:41
  • The first time this command I've suggested will copy everything because it needs to catch up with the timestamps, and copying between two local filesystems makes checksum validation pointless in the face of a simple copy. The second time the command runs it will skip anything and everything it reckons hasn't been changed. Why not try it on a small subset of your files, or better still on a throw-away sample of them
    – roaima
    Jun 7 at 13:09

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.