Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-20-2012, 03:11 AM   #1
LQ Newbie
Registered: Jun 2012
Posts: 2

Rep: Reputation: Disabled
Bash quesion - scp on multiple hosts to multiple directories in each one

Hello everyone,

I am a beginner in bash and I am trying to write
a script that scp's a file from my local host to multiple servers (and on each server to multiple directories)
My script so far looks something like:

servers=(test1 test2 test3 test4)
echo "Number of servers: $COUNTER "
for (( c=0; c<$COUNTER; c++ ))
echo "server ${c}: ${servers[${c}]} "

echo /opt/blah/*/test/ | xargs -n 1 scp user1@${servers[${c}]}:file.txt
echo "server ${c} : ${servers[${c}]} --done "

i know my xargs command is wrong and i dont quite understand how this command works in order to make my script work.

Any help would be appreaciated.

Thank you
Old 06-20-2012, 03:54 AM   #2
LQ Newbie
Registered: Jun 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Well i solved the problem by using
two for-loops : one to go thru server and one for generating a subdirectory list and scp-ing in them.

Thank you anyway guys.

But if someone could show me how the xargs work in this particular case I would appreciate it.

Old 06-22-2012, 12:27 PM   #3
Senior Member
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.0
Posts: 1,336

Rep: Reputation: 255Reputation: 255Reputation: 255
What errors do you get? You want to copy one and the same file file.txt from a remote location to several locations at the target side? The default remote location used will be your home directory. You can prefix the scp with an echo to see the generated line.

NB: Array indices don’t need another curly braces, they can be referred as ${servers[c]}


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple scp/ssh in one bash script Lovelyhard Programming 13 07-31-2012 09:37 AM
scp file to multiple hosts with expect loadedmind Programming 4 04-11-2011 09:28 PM
[SOLVED] Bash backup script with scp, multiple processes mky Programming 8 09-29-2010 06:28 PM
Deleting remote directories on multiple hosts with a bash script zirtik Programming 6 04-16-2009 09:52 AM
create multiple directories in bash? Count Zero Programming 19 07-06-2008 08:34 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:23 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration