Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can you scp a file to multiple locations in the same command?

For example:

scp file.txt user@ip-1.com:, user@ip2.com:

Or would it be more practical to create a bash script that has all the hosts already in it and it would just take a file as the argument?

share|improve this question
up vote 12 down vote accepted

Let's say you have a file (destfile.txt) with user@host-values, one on each line. Then you could do like this:

for dest in $(<destfile.txt); do
  scp ourfile.txt ${dest}:remote/path/
done
share|improve this answer
    
This works! Still a handful to type out though. Perhaps there's a way to simplify that. – Andrew Sep 22 '11 at 21:02
1  
@Andrew, if you change the for-loop into while read dest; do, it will read from standard input. Put it in a script and feed the destfile.txt into it (e.g., ./thescript.sh <destfile.txt). – Kusalananda Sep 22 '11 at 21:19
    
Thank you. That does simplify it a bit. – Andrew Sep 22 '11 at 21:24

Looks like a job for parallel-scp - this implements a set of commands that allow for scp commands to be run on multiple systems at once. It will allow for the copying of files in parallel to a set of machines.

share|improve this answer
cat file.txt | tee >(ssh user@ip1.com "cat > file.txt") \
                   >(ssh user@ip2.com "cat > file.txt")

tar cz file1 file2 file3 | tee >(ssh user@ip1.com "tar xz") \
                               >( ... )
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .