27

Is it possible to use rsync to copy files in one direction only?

For example, suppose we have:

left/a.txt

right/a.txt

where the files are initially identical.

If one then modifies right/a.txt, then:

rsync -avv left/ right/

will copy right/a.txt onto left/a.txt.

Is it possible to restrict rsync to only copying from left/ to right/ (i.e. prevent it from copying from right/ to left/)?

artella
  • 959
  • 2
  • 9
  • 6

2 Answers2

43

You misunderstand rsync. This command:

rsync -avv left/ right/ 

will not sync anything in right to left. It will, as @atbg says, only sync left to right. Rsync is not a bi-directional syncer. It syncs the dest with the source.

Man page for reference: http://linux.die.net/man/1/rsync

jdw
  • 3,735
  • 1
  • 17
  • 20
  • Interestingly when operating at least the Mac version of rsync to sync remote directory via ssh it is bidirectional - e.g. `rsync -avzz -e "ssh -p 2222" /source/ /target/` – geotheory Oct 25 '19 at 07:30
4

It should be rsync [OPTION...] SRC... [DEST] so it does work in that direction (unless you switch dest and src).

left/a.txt should be copied to right/a.txt:

$ echo 'left' > left/a.txt
$ echo 'right' > right/a.txt
$ cat left/a.txt && cat right/a.txt
left
right
$ rsync -avv left/ right/
sending incremental file list
delta-transmission disabled for local transfer or --whole-file
a.txt
total: matches=0  hash_hits=0  false_alarms=0 data=5

sent 95 bytes  received 34 bytes  258.00 bytes/sec
total size is 5  speedup is 0.04
$ cat left/a.txt && cat right/a.txt
left
left

If there are specific files you don't want included by rsync take a look at --exclude=PATTERN and --exclude-from=FILE.

agtb
  • 226
  • 2
  • 8
  • 3
    And remember, when in doubt the `--dry-run` option will show you a list of the files that would have been transferred but not actually perform the transfer. I add this to the start of every `rsync` just as a precaution, even if I am sure the command is correct. – slillibri Oct 18 '11 at 22:03
  • 1
    Given the confusion about rsync's operation, it is perhaps worth noting rsync's `--update` option, which will skip (not sync) files which have a more recent `modified` time on the receiving side. – Richard Michael Dec 29 '13 at 22:17