1

I want to copy and overwrite only files that exist in the destination. Aka excluding 'lonely files' that exist in the source xor the destination.

2 Answers2

1

You should be able to achieve this with rsync, using the --existing flag:

        --existing              skip creating new files on receiver

Ex. given

$ tree -a A/ B/
A/
├── bar
├── baz
├── foo
└── .hidden
B/
├── bam
├── baz
├── foo
└── .hidden

0 directories, 8 files

where baz, foo and .hidden are common but bar exists only on the source and bam only on the target, then (note: I've included the verbose -v and -n "dry run" flag for the purpose of illustration):

$ rsync -avn --existing A/ B/
sending incremental file list
.hidden
baz
foo

sent 152 bytes  received 25 bytes  354.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)

only updates baz, foo and .hidden. (Files that exist only on the destination are ignored by default.)


You can verify that without --existing, bar would also be copied:

$ rsync -avn A/ B/
sending incremental file list
.hidden
bar
baz
foo

sent 155 bytes  received 28 bytes  366.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)
steeldriver
  • 136,215
  • 21
  • 243
  • 336
0

Check this out on stackoverflow. There are several solutions. I didnt try them but I think they should work and solve your problem:

https://stackoverflow.com/questions/47154327/move-or-copy-a-file-if-that-file-exists?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

Hope it helps you. ;)