rsync --delete-existing needed

Matthias Schniedermeyer ms at
Sat May 5 22:43:01 GMT 2007

Erik Red wrote:
> Hi,
> I made the mistake of doing an rsync of a directory into the wrong
> destination,
> so that the destination became a mix of two directories of unrelated files.
> Top unravel the mess, I could need something like a --delete-existing
> option.
> The semantics would be to delete at the destination ONLY those files
> that exists at the
> source.
> I tried to concoct a combination of options that would do this, but
> without success. I thought someone here might know the answer. Or would
> this be a handy feature to add?
> [One should of course use --dry-run and check the result before doing
> such an operation.]

I would suggest a bit of good old shell magic here as it is, or at least should be, some case of an one-time emergency.

Make a list of all the files on the source-side:
find . -type f -o -type l > files.txt
or (If you have whitespaces)
find . -type f -o -type l | tr [\\n] [\\0] > files0.txt

Then you copy over the list and and use it to kill all the files in it:
xargs rm < files.txt
or (Whitespaces)
xargs -0 rm < files0.txt

If you have subdirectories to be killed:
find . -type d -empty -exec rmdir {}\;
You have to run that a few times (One time for each deepth-level), and don't worry about the 'No such file or directory' warnings that it will spit for every deleted directory.
Find just can't enter the directory it had deleted a nanosecond ago.

Or, if you had empty directory on the target you don't want to be killed
find . -type d | sort -r > dirs.txt
(Thorough in the "tr" from above if you have whitespaces)
And then
xargs rmdir < dirs.txt
(Add the -0 if added the "tr")

Bis denn

Real Programmers consider "what you see is what you get" to be just as
bad a concept in Text Editors as it is in women. No, the Real Programmer
wants a "you asked for it, you got it" text editor -- complicated,
cryptic, powerful, unforgiving, dangerous.

More information about the rsync mailing list