Para se preceder à copia rápida de um ou mais ficheiros entre pcs via ssh temos pelo menos duas formas:

i) scp
ii) ssh directo a partir do computador para onde se vão copiar os ficheiros (ssh user@host “cat /path/file_a_copiar” > file_destino)

 

Copying Files with scp (link)

The OpenSSH suite of programs is one of my favourite toolkits for administration of servers on a LAN. I routinely use the scp command to copy files between systems and move stuff around as required. In effect, it replaces the old rcp command, but it much more secure as well as more convenient to use.

To copy files between two machines, say 192.168.1.101 and 192.168.1.100, sit at 192.168.1.101 and use the following command:

scp * 192.168.1.100:

Simple as that! Assuming you are the same user id on both machines, this will copy all files in the current directory to your home directory on the destination machine, 192.168.1.100. The first thing the command will do, though, is ask you for your password on the remote system – once you supply that, then you’ll see the files copied, with progress bars.

Now, if you want to copy only some files, e.g. all txt files, use a standard wildcard, like this:

scp *.txt 192.168.1.100:

Suppose you want to copy them to a destination directory other than your home directory, use:

scp *.txt 192.168.1.100:/home/username/directory

Of course, you have to have write permission on the target directory.

Suppose you want to copy files from the other machine back to the one you’re on – then use this syntax:

scp 192.168.1.100:*.txt .

If you have a DNS or hosts file set up, then you can (and should) use hostnames in the command, like this:

scp mail/* mailsrvr:/home/joe/mail

This will copy the contents of the mail subdirectory (of the current directory) on this machine, to the directory /home/joe/mail on the machine mailsrvr.

How Does It Work?

In general, the syntax for scp (as for cp) is:

scp [option...] source destination

where source and destination can each take the form:

[hostname:][dir-path][filespec]

or

[ip-addr:][dir-path][filespec]

The [ ] indicates something is optional. The big difference from the cp command is the use of a hostname or IP address on either the source, destination or (unusually) both. Notice that the hostname or IP address must be followed by a colon; a common mistake (I do it all the time) is to type something like:

scp fubar.zot 192.168.1.100

but the file doesn’t turn up on 192.168.1.100. So what happened? Answer: you created a file called “192.168.1.100” that contains the same thing as fubar.zot!

If you find the password prompting is a nuisance, then you can create a private/public key pair, upload the public key to the remote system, and then use the SSH agent to supply your private key automatically.

 

Advertisements