Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using rsync to delete a single file

Tags:

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?

like image 341
dirtside Avatar asked Feb 02 '09 22:02

dirtside


People also ask

Can rsync delete files from source?

For removing the files from the source after the transfer, the rsync command provides the –remove-source-files option.

Does rsync delete old files?

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 .

How do I delete a source file?

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.

Is rsync efficient?

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.


2 Answers

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"
like image 112
David Z Avatar answered Oct 01 '22 11:10

David Z


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.

like image 25
letitbee Avatar answered Oct 01 '22 11:10

letitbee