Monday, August 06, 2007

Copying multiple files with SCP

I needed to copy several files from one remote host to another. To that end I decided to use scp, but if I simply write one "scp" command for each file I must provide the password each time I enter a command, besides having to repeat the command several times. Fortunately this can be done in a more efficient way. Indeed it is possible to use normal shell expansion, just make sure you use quotes to ensure that it isn’t parsed and expanded by the local shell instead of the remote one. Thus, the command should look like:

% scp me@host:"teste1.zip,teste2.zip,teste" .

10 comments:

Thangasivam said...

Dear Alfredo Ferreira,
I have tried this following command and it doesn't work for me
scp me@host:"teste1.zip,teste2.zip,teste" .
but this works
scp me@host:"teste1.zip teste2.zip teste" .

And If I want to copy files from directory other than home folder, please help me. above commands are not helping me for this case.
Sivam

Wireball said...

I was successful with:

scp me@host:/remote/directory/\{file1,file2,file3\} .

(note the backslash escapes before the curly braces)

This worked on FreeBSD, CentOS, and Ubuntu.

mercury said...

Following works perfectly while connecting from "HP-UX B.11.11" to "SunOS 5.10"

LIBP=/path/on/remote/machine
scp USER@REMOTE:"$LIBP/file1.jar $LIBP/file2.jar $LIBP/../file3.jar" /path/on/local/machine

ICTgeek said...

Using the following to connect from Mac OS X to Linux works fine:

scp user@RemoteHost:path/\{file1.txt,file2.txt\} ~/local/path

Nandy said...

scp user@RemoteHost:path/\{file1.txt,file2.txt\} ~/local/path

is working fine on my machine..RHEL 4

Unknown said...

Thank you very much Wireball for providing this scp command, i was looking for this command from many days. -- Ravi Mishra

Veda said...

Wireball's command is working between cygwin (windows) to Linux.

Devika Singh said...

Good one !.. Like the description and the link

Unknown said...

I have tried this with a list of files. It works fine unless 1 is missing, then it stops completely and won't copy any more files.

Is there a way to override this behavior so it just skips missing files and carries on with the copy?

Craig said...

I have written two scripts and this blog helped me.

I had a script for Linux in which this worked perfectly:

scp user@remote:path/\{file1.txt,file2.txt\} ~/local/path

however this did not work when the remote host was UNIX, specifically AIX.

thanks to mercury I was able to modify my script to work with AIX via:

LIBP=/path/on/remote/machine
scp USER@REMOTE:"$LIBP/file1.jar $LIBP/file2.jar $LIBP/../file3.jar" /path/on/local/machine

thanks mercury