2

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?

3

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

  • 1
    Yeah checksum seems like the more accurate approach since we usually care about file contents.. – Alexander Mills Aug 14 '18 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! – alexmherrmann Aug 14 '18 at 4:13
  • Ah yes, you are probably right – Alexander Mills Aug 14 '18 at 21:15

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.