How can I copy multiple files over scp in one command?

Use tar:

tar cvzf - -T list_of_filenames | ssh hostname tar xzf -

I would like to use scp for that, but scp only allows for transferring one file at a time.

I'm pretty sure that isn't true, at least not for the scp command provided by the OpenSSH included with most Linux distributions.

I use scp file1 file2 ... fileN user@host:/destination/directory/ fairly frequently.

For transparent compression, the SSH protocol has this built in and scp can use it if you provide the -C option on the command line. For lots of similar small files you will find the tar+gz option suggested by akira gains better compression as it can make use of similarity between such files where scp compresses each file as a separate entity. I generally prefer to use scp though as it is easier to resume a partial transfer (or, though I know in this situation the questioner doesn't have the option) rsync as that is both even easier to resume and shares the tar+gz option's whole-stream compression advantage.


Doesn't scp -r yourdir otherhost:/otherdir work?

Try this then:

tar cfz - . | ssh otherhost "cd /mydir; tar xvzf -"

the z-flag to tar does compression. Or you can use -C to ssh:

tar cf - . | ssh -C otherhost "cd /mydir; tar xvf -"

Tags:

Linux

Ssh

Scp