I have a folder on server "A" called ./delete.

I want to delete everything in the folder ./stuff on server "B" that exists in the ./delete folder on server "A".

Its like the reverse of rsync. instead of transferring files from a:/delete to b:/stuff I want to just delete all the files that are in both places on server "B".

like a --delete-on-destination flag or something.

3 Answers 3


File copy tools such as rsync aren't going to help you easily since you don't want to copy any files.

The straightforward approach of listing the files on server A and erasing these files on server B is a good one in most circumstances. It's easier to cope with arbitrary file names if your servers' find and xargs commands understand null separators (Linux, *BSD, Cygwin). From A:

cd ./delete
find . ! -type d -print0  | ssh B 'cd /path/to/stuff && xargs -0 rm -f'

This may leave some empty directories behind. You can delete all empty directories (even the ones that were empty before) with

ssh B 'cd /path/to/stuff && find . -depth -type d -exec rmdir {} + 2>/dev/null'

If you only want to remove directories that existed on the source side, you'll need to use the list again:

find . -depth -type d -print0  | ssh B 'cd /path/to/stuff && xargs -0 rmdir'

If there are directory trees on A that contain a lot of files and don't exist on B, this transfers the whole list of files to erase where a well-chosen rm -rf on B would do the same work locally on B but would save a lot of bandwidth in the transfer. This is the kind of situation where a file synchronization tool would fare well. You could run rsync -nv and try to parse the output, but it isn't easy to build something reliable on this.

  • this only works on files. not directories, and I believe it deletes the files from A, not B.
    – chovy
    Aug 22, 2013 at 5:32
  • 1
    @chovy Indeed, I'd misread your question. I don't think file synchronization tools will help much since you aren't copying files. Although, hmmm, there might be a way to build something with rsync --remove-source-files --compare-dest Aug 22, 2013 at 23:33
  • i don't understand what --compare-dest does.
    – chovy
    Aug 24, 2013 at 21:13

I wanted to do this also, and decided to just write a ruby script for it.

Here you go:



I don't think that rsync can resolve your problem. But another sync tool might be able to help you. Take a look at unison.

Unison has many features and can sync two folders on different machines. Unison is written in OCaml that is a charming functionional language.

Rsync is a unidirectional sync tool.Unison is a bidirectional sync tool.

  • thanks. i used a series of scripts to accomplish what I wanted, but this might work.
    – chovy
    Aug 21, 2013 at 7:46
  • How would Unison help here? The purpose isn't to synchronize the two directories at all. Aug 21, 2013 at 22:26
  • after looking at unison, I figure it was just a blanket answer without really reading the problem I am faced with.
    – chovy
    Aug 22, 2013 at 5:33
  • Unison can keep the two folder on different machine sync.If clientA delete some files,clientB will delete some files too! Aug 23, 2013 at 1:49
  • @chovy It looks like this doesn't actually answer your question; would you consider unaccepting the answer to avoid giving other readers the impression that it does?
    – David Z
    Dec 21, 2020 at 12:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .