4

Right now I have these commands:

my_dir="$HOME/foo/bar"; rm -rf "$my_dir/WebstormProjects/"; rsync -r --exclude=".git" --exclude="node_modules" "$HOME/WebstormProjects/" "$my_dir/WebstormProjects/" 

instead of removing everything in "$my_dir/WebstormProjects/" and then copying over, I am looking to use rsync such that it will only overwrite files in "$my_dir/WebstormProjects/" if the files in "$HOME/WebstormProjects/" are newer. So something like this:

my_dir="$HOME/foo/bar"; rsync -r --newer --exclude=".git" --exclude="node_modules" "$HOME/WebstormProjects/" "$my_dir/WebstormProjects/" 

is it possible to copy files only if they are newer than the destination files?

I did some reading: https://www.tecmint.com/sync-new-changed-modified-files-rsync-linux/

it says:

By default, rsync only copies new or changed files from a source to destination...

I assume it does this by comparing relative file paths? Files with the same path/name are considered to be the same?

1
  • Keep reading; rsync will compare timestamps as well as hashing the files. See serverfault.com/a/211083 Commented Aug 14, 2018 at 0:22

1 Answer 1

7

This is the rsync default behavior. It checks file size and last modification size.

If you want it to use checksums instead, use the -c or --checksum option (same effect). The man page is here and describes the option, also of interest might be --size-only

4
  • 2
    Yeah checksum seems like the more accurate approach since we usually care about file contents.. Commented Aug 14, 2018 at 3:55
  • @AlexanderMills Yes, it is more accurate. Just be aware that for large files it can get very slow (since it has to read and run a one-way hash on the ENTIRE file). If the size, name, and modified time of a file are all the same, the file has probably not updated. Unless you run into or really anticipate a problem with those causing issues you're probably just better off using rsync's sane defaults. Happy coding! Commented Aug 14, 2018 at 4:13
  • Ah yes, you are probably right Commented Aug 14, 2018 at 21:15
  • @alexmherrmann I believe that rsync checks the file size first. If the size has changed, then it skips the checksum hash, rightly assuming that the checksum has also changed. Commented Feb 4 at 19:52

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.