Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote -1 down vote accepted

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.

share|improve this answer
thanks. i used a series of scripts to accomplish what I wanted, but this might work. – chovy Aug 21 '13 at 7:46
How would Unison help here? The purpose isn't to synchronize the two directories at all. – Gilles Aug 21 '13 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 '13 at 5:33
Unison can keep the two folder on different machine sync.If clientA delete some files,clientB will delete some files too! – Edward Shen Aug 23 '13 at 1:49

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.

share|improve this answer
this only works on files. not directories, and I believe it deletes the files from A, not B. – chovy Aug 22 '13 at 5:32
@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 – Gilles Aug 22 '13 at 23:33
i don't understand what --compare-dest does. – chovy Aug 24 '13 at 21:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.