Using rsync to delete a single file

rsync

File foo.txt exists on the remote machine at: /home/user/foo.txt

It doesn't exist on the local machine.

I want to delete foo.txt using rsync.

I do not know (and assume for the purposes of this question that I cannot find out) what other files are in /home/user on either the local or remote machines, so I can't just sync the whole directory.

What rsync command can I use to delete foo.txt on the remote machine?

Best Answer

Try this:

rsync -rv --delete --include=foo.txt '--exclude=*' /home/user/ user@remote:/home/user/

(highly recommend running with --dry-run first to test it) Although it seems like it would be easier to use ssh...

ssh user@remote "rm /home/user/foo.txt"