Using rsync to delete a single file

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"

That's a bit trivial, but if, like me, you came to this page looking for a way to delete the content of a directory from remote server using rsync, this is how I did it:

  1. Create an empty mock folder:

    mkdir mock

  2. Sync with it:

    rsync -arv --delete --dry-run ~/mock/ remote_server:~/dir_to_clean/

  3. Remove --dry-run from the line above to actually do the thing.

Tags:

Rsync