70

I want to download a large amount of data from a remote machine. I'd like the data to be erased on the remote machine everytime a file finishes downloading.

How do I do that? Is there a flag for rsync to do this?

eran
  • 14,496
  • 34
  • 98
  • 144

1 Answers1

99

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. Do not pass the --delete option to rsync command as it delete extraneous files from destination directory. Delete source after successful transfer using rsync.

The syntax is:

rsync --remove-source-files -options /path/to/src/ /path/to/dest
rsync --remove-source-files -options /path/to/src/ computerB:/path/to/dest
rsync --remove-source-files -av /path/to/src/*.avi computerB:/path/to/dest

Reference : http://www.cyberciti.biz/faq/linux-unix-bsd-appleosx-rsync-delete-file-after-transfer/

Worthwelle
  • 1,244
  • 1
  • 16
  • 19
ganeshragav
  • 8,695
  • 1
  • 16
  • 13
  • 9
    Is there a way to also delete directories from the source side? – Johann Bzh Feb 07 '19 at 07:53
  • 5
    This is helpful too: https://explainshell.com/explain?cmd=rsync+--dry-run+--remove-source-files+-avzPh – Tung Feb 21 '19 at 13:10
  • 1
    explainshell.com is a beautifully-done tool, thanks for that tip. – whiskeychief May 01 '21 at 12:20
  • 3
    See also this very helpful post on Unix StackExchange about removing directories as well: https://unix.stackexchange.com/questions/371144/usage-of-remove-source-files-option-of-rsync – GDP2 May 08 '21 at 18:32
  • Is there a way to **always** delete source files i.e. even when their transfer to destination fails? – Petr Vepřek Sep 21 '21 at 11:57
  • rsync is for actions related to duplication/mirroring being successful. If you want to purge any remaining after the rsync job is done, that is a job for basic rm. – Eric Marceau Sep 25 '22 at 01:24