11

I am writing a script that will move files from a local system to a remote system. It must do so through an encrypted channel like ssh. What is the best way to do this? I can perform this in two steps like:

scp *.jpg user@ip:
rm *.jpg

But, that is not an atomic process (like mv is for a local filesystem). If the copy fails I will no longer have the local copies either. How can I script this to make sure the local files only get removed if the copy succeeds?

Chris Maes
  • 35,025
  • 12
  • 111
  • 136

2 Answers2

16

You could use rsync with --remove-source-files:

rsync -avz --remove-source-files /local/dir/*.jpg user@ip:/remote/dir 
Josh Jolly
  • 11,258
  • 2
  • 39
  • 55
1

An other solution, for launch in one time

scp /path/src/*.jpg user@host:/path/dst/ && rm /path/src/*.jpg
openghost
  • 62
  • 3
  • 4
    Note when doing this that if new files are created in `/path/src/` _during_ the scp copy command they will be deleted and not copied. – Dzamo Norton Jun 26 '17 at 11:21
  • Very risky, if scp command terminate for any reason, files car be deleted without having being copied. – Golgot Jan 23 '23 at 09:02