12

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?

6
  • 1
    I'm afraid you can't, because quite like cp, scp may take multiple sources but only one destination (which must be a directory when using multiple sources). You'd have to use multiple scp commands. Is there any reason you want to use just one scp command? Commented Jul 14, 2013 at 15:19
  • @Julien: Obviously, if multiple destinations are on the same remote host, you don't want to transfer the data to that host multiple times. At that point, probably better to scp once and then ssh a copy command... does scp or sftp actually have that ability built in, to command the remote computer to make a copy on itself? Of course, that does introduce a race condition,so you might actually copy over-the-network to a unique temporary location, then have the remote copy from there into all the desired destinations. Commented Jul 14, 2013 at 15:36
  • But scp is not a programming language. Commented Jul 14, 2013 at 15:39
  • 1
    Good point if it's about one file to multiple destinations, but as you figured out yourself, it's about multiple files each to its one of multiple destinations, so you would have to do each transfer anyway! Commented Jul 14, 2013 at 15:59
  • @Julien: Yes I have a reason that I want to use only one scp. I am a system admin and developers provides me locations of files. Using only one scp means I can easily use them with my python script. Thanks for replying. Commented Jul 14, 2013 at 16:13

7 Answers 7

15

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Your "one-liner" is what I was looking for. Thanks. I have to use this in a python script, so I can't run the command everytime.
One other point. It is a highly recommended best practice not to have "root" open to login via SSH. Check out sudo for the recommended way to allow another use to read/write/run whatever you need.
4

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

Comments

2

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

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

1 Comment

Oh good catch, the OP said only one file to each destination, I'd missed that at first.
0

you can build the desired remote tree structure using sym links for example

ln -s file1 target/location1/file1 ... etc

after this you can use a dereferencing copy to push these files

rsync -rL target -e ssh user@host:/tmp

Comments

0

Hi there is a Hack to go about this query since SCP targets only single Destination, but warn you this might be a bit Naive but gets your work done. use the && operator between two SCP calls

I use it like this:

scp fileTosend userName1@IPAddress_1:path/to/folder && scp fileToSend userName2@IPAddress_2:path/to/folder

This will simultaneously send the data to both the destinations.

Comments

0

I've had the same issue. i did solve it somewhat by adding a second part where an ssh connection is made and the files are moved from /var/tmp to their target dir. It's a bit hacky but it works and if you put it in a bash script it is only a single line

Comments

0

You can use an sftp batch (if you are OK using sftp instead of scp). This way you establish a single ssh connection and do all the transfers over it.

E.g.:

sftp -b - [email protected] <<EOF
put file1 /location1
put file2 /location2
EOF

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.