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.
Asked
Active
Viewed 1,002 times
2 Answers
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
-
Just to make sure, does this copy hidden files as well? – Emergency Temporal Shift May 02 '18 at 21:46
-
@EmergencyTemporalShift yes I believe it will - see updated answer – steeldriver May 02 '18 at 22:17
0
Check this out on stackoverflow. There are several solutions. I didnt try them but I think they should work and solve your problem:
Hope it helps you. ;)

winnetou
- 1