How to not copy but move files from one server to another (both Linux)?
man scp
didn't give me anything useful. I cannot use 'scp' then 'rm' because I must make sure the file is successfully transferred. If there is any error during transfer, the file must not be deleted.
Perhaps I should use exit code somehow, but how? Also, there are a lot of files, and if the last file fails it would be not-so-good option keep the whole bunch of successfully transferred files.
Maybe there is something besides SCP?
rsync over ssh is probably your best bet with the
--remove-source-files
optiona quick test gives;
As @SvenW mentioned,
-e ssh
is the default so can be omitted.Use
rsync
instead ofscp
:More info with
man rsync
.This question's been answered just fine, and the answer accepted, but since it's floated to the top of the front page, I thought I'd at least try to answer it more precisely, if less elegantly. Yes, you can use the return code from
scp
, and I do it often. Inbash
:I take your point about multiple files to copy and handling failure down the stack correctly, so for multiple files:
This last is only practical if you're using
ssh-agent
, but I very much hope you are.in my situation ,ssh port is not 22, so
works for me.
if you have older target server as I do, you can't use
but you have to use
instead.
If doing it in two steps is not a problem, you can use
scp
to copy the file from the remote server, and then executessh -e "rm /path/to/file"
to remove from disk. When moving files especially between machines things may go wrong, so it may be good to perform the copy and the delete separately, and only remove the file when you know for sure that it has been successfully copied first.Thought I'd suggest an alternative to rsync I found, lftp, because rsync requires shell access which my sftp server blocks.
You can also pass in the password if you need to run this as part of a batch process / cron job (obviously insecure)