Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send multiple file to multiple location using scp

Tags:

linux

I need to send multiple files to multiple location, but can't find the proper way.

e.g. I need to send file1 to location1 and file2 to location2. This is what I am doing:

scp file1 file2 [email protected]:/location1 /location2

But this is not working. Any suggestion?

like image 862
Abhishek dot py Avatar asked Jul 14 '13 15:07

Abhishek dot py


3 Answers

It's not possible to send to multiple remote locations with a single scp command. It can accommodate multiple source files but only one destination. Using just the scp command itself, it must be run twice.

scp file1 file2 [email protected]:/location1 
scp file1 file2 [email protected]:/location2

It is possible to turn this into a "one-liner" by running a for loop. In bash for example:

for REMOTE in "[email protected]:/location1" "[email protected]:/location2"; do scp file1 file2 $REMOTE; done

scp still runs multiple times but the loop takes care of the iteration. That said, I find it easier to run the command once, hit the Up Arrow (which brings the original command back up in most environments) and just change the remote location and resend.

like image 86
Alan W. Smith Avatar answered Oct 13 '22 22:10

Alan W. Smith


you can put your scp command in .sh file and execute scp commands with the user name and password in one line

vim ~/fileForSCP.sh

#!/bin/sh
sshpass -p {password}   scp    file1     [email protected]:/location1 
sshpass -p {password}   scp    file2     [email protected]:/location2
sshpass -p {password}   scp    file3     [email protected]:/location3
...
sshpass -p {password}   scp    file{n}   [email protected]:/location{n}


and then:

chmod 777 ~/fileForSCP.sh
~/fileForSCP.sh
like image 25
Ali Rezvanian Avatar answered Oct 13 '22 22:10

Ali Rezvanian


You cannot do it with single scp command. Just use scp twice:

scp file1 [email protected]:/location1
scp file2 [email protected]:/location2
like image 40
nullptr Avatar answered Oct 13 '22 20:10

nullptr