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?
For removing the files from the source after the transfer, the rsync command provides the –remove-source-files option.
Show activity on this post. By default rsync doesn't delete any files at the destination side. To make rsync delete files at all, you need to use at least one of the delete options. If you don't care when files are being deleted, just use --delete and leave the choice to rsync .
You need to pass the --remove-source-files option to the rsync command. It tells rsync to remove from the sending side the files (meaning non-directories) that are a part of the transfer and have been successfully duplicated on the receiving side.
The rsync algorithm efficiently computes which parts of a source file match parts of an existing destination file. Matching parts then do not need to be sent across the link; all that is needed is a reference to the part of the destination 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:
Create an empty mock folder:
mkdir mock
Sync with it:
rsync -arv --delete --dry-run ~/mock/ remote_server:~/dir_to_clean/
Remove --dry-run
from the line above to actually do the thing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With