4

man synopsis for scp is:

scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 ... [[user@]host2:]file2

What is source, what is target? I'm always wondering in which direction the copying takes place. How is it defined?

0

3 Answers 3

8

Unix commands almost always (with very few exceptions) have source before target. And most allow multiple sources before the final target if it makes sense to do so.

That includes scp.

Some commands (like the GNU versions of cp and mv) have an option (e.g. -t or --target-directory=DIRECTORY) that allow you to put the target first - but the default is the standard "source(s) before target". This is mostly useful so you can have the target before the source when using something like find ... -exec cp -t target/ {} +, or so you can avoid using -I {} with xargs (which is much slower, it causes xargs to fork one command per arg, rather than fit as many as it can on each command line), e.g. ... | xargs -0r mv -t target/ instead of ... | xargs -0r -I {} mv {} target/

Without an explicit override option as mentioned above, "target before source" is almost unheard of - so rare that you're fully entitled to think "Huh? WTF?" if you ever encounter it.


If you need to scp multiple files to multiple machines at once, do it in a for loop like this:

for h in host1 host2 host3... ; do
  scp file1 file2 file3... "user@$h:/path/"
done

Or use pdcp from Parallel Distributed Shell. pdsh is packaged for most linux distributions.

e.g.

pdcp -w host1,host2,host3 file1 file2 file3 /path/to/target/

That's a very simple example, pdsh has a config file you can use to define hosts and groups of hosts with arbitrary group names (e.g. all, webservers, dbservers, gpunodes, or whatever). You can then use them in pdsh or pdcp commands:

pdsh -g all 'uname -a ; uptime'
pdcp -g webservers index.html /var/www/

Unlike the for loop above, it doesn't run commands or copy the files sequentially, one host at a time. It runs multiple commands in parallel, with reasonable limits based on the number of CPU cores your machine has.

4
  • And I can have multiple sources or multiple targets with scp?
    – robsch
    Jul 5, 2016 at 7:43
  • 2
    multiple sources, yes. multiple targets, no. run scp multiple times with different targets (or use something like pdcp from pdsh) if you want that.
    – cas
    Jul 5, 2016 at 7:43
  • @robsch as cas suggested, you can have multiple sources, like scp file1.sql file2.sh user@remote:~/upload
    – Rahul
    Jul 5, 2016 at 7:46
  • 1
    btw, pdsh (parallel distributed shell) is packaged for most distros. source is at github.com/grondo/pdsh. home page used to be at llnl.gov (they developed it for use with HPC clusters, and it looks like the primary dev still works at LLNL) but the pdsh home page URL now just redirects to some usage info for their clusters.
    – cas
    Jul 5, 2016 at 7:48
2

With the scp command, you can specify the source (the file or directory to be copied) and the target (the location in which to copy the file or directory). The syntax for scp is:

If you are on the computer from which you want to send file to a remote computer:

scp /file/to/send username@remote:/where/to/put

Here the remote can be a FQDN or an IP address.

On the other hand if you are on the computer wanting to receive file from a remote computer:

scp username@remote:/file/to/send /where/to/put
2
  • So it's scp from to?
    – robsch
    Jul 5, 2016 at 7:39
  • @robsch yes, basic syntax says that merely. copy from /path to /path/
    – Rahul
    Jul 5, 2016 at 7:41
0

You can infer it from the ... part.

You can copy multiple source files, but only to a single target. The ... means you can specify additional files at this position. Since the ... is given directly after file1, this must be the source.

Example: scp fileA fileB fileC would copy fileA and fileB to fileC.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.