How to _MOVE_ files with scp?

Solution 1:

rsync over ssh is probably your best bet with the --remove-source-files option

rsync -avz --remove-source-files -e ssh /this/dir remoteuser@remotehost:/remote/dir 

a quick test gives;

[tomh@workstation001 ~]$ mkdir test1
[tomh@workstation001 ~]$ mkdir test2
[tomh@workstation001 ~]$ touch test1/testfile.1
[tomh@workstation001 ~]$ ls test1/
testfile.1
[tomh@workstation001 ~]$ rsync --remove-source-files -av -e ssh test1/testfile.1 tomh@localhost:/home/tomh/test2/
sending incremental file list

sent 58 bytes  received 12 bytes  10.77 bytes/sec
total size is 0  speedup is 0.00

[tomh@workstation001 ~]$ ls test1/
[tomh@workstation001 ~]$
[tomh@workstation001 ~]$ ls test2/
testfile.1

As @SvenW mentioned, -e ssh is the default so can be omitted.

Solution 2:

Use rsync instead of scp:

rsync -avz --remove-source-files /sourcedir user@host:/targetdir 

More info with man rsync.


Solution 3:

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. In bash:

scp foo user@server:/destination && rm foo

I take your point about multiple files to copy and handling failure down the stack correctly, so for multiple files:

for file in bar*; do scp "$file" user@server:/destination && rm "$file" ; done

This last is only practical if you're using ssh-agent, but I very much hope you are.


Solution 4:

in my situation ,ssh port is not 22, so

rsync -avz --remove-source-files -e "ssh -p $portNumber" user@remoteip:/path/to/files/ /local/path/

works for me.


Solution 5:

if you have older target server as I do, you can't use

--remove-source-files

but you have to use

--remove-sent-files --protocol=29

instead.