Friday, August 31, 2012

SCP command for secure copy or transfer of files between two machines or servers.


Sometimes it is required to copy files from or to a remote server. Sometimes you may also want to copy files from one remote server to another remote server, without passing traffic through your PC. scp command line utility is a neat tool/command to securely copy files over ssh, between Linux, Mac or Windows.

It is important to note here that scp stands for secure cp (copy). It means that you can copy files across ssh connection. The connection will be securely encrypted, it is a very secure way to copy files between computers

Syntax of SCP command:

scp [[user@]from-host-addr:]source-file [[user@]to-host-addr:][destination-file]

from-host-addr
Is the name or IP of the host where the source file is, this can be omitted if the from-host is the host where you are actually issuing the command
user
Is the user which have the right to access the file and directory, that is supposed to be copied in the case of the from-host, and the user who has the rights to write in the to-host
from-host-addr
Is the name or IP of the destination where the source file is, this can be omitted if the from-host is the host where you are actually issuing the command
source-file
Is the file or files that are going to be copied to the destination host, it can be a directory but in that case you need to specify the -r option to copy the contents of the directory
destination-file
Is the name that the copied file is going to take in the to-host, if none is given all copied files are going to keep its names

There are a few options which you can explore on your own. Like –p , -q, -r (for directory) and –v etc.

My small example:
scp -r user@x.x.x.x:/anwar/jamal/faiz /destination/folder/on/present/machine
Then it asks for password for user on machine x.x.x.x.
Give that. And, see task being accompalished!!

Some quick SCP commands variation that I use are as follows:

Following is going to copy all files with .txt extension to the folder /home/user in the abc.server.com host
scp *.txt user@abc.server.com:/home/user/

Following is going to copy all file from server to ur computer
scp username@server:/home/username/file_name /home/local-username/file-name

Following is used to copy files from a remote server to another remote computer
scp user_name1@server1:/home/user_name1/file_name user_name2@server2:/home/user_name2/
In above command files are copied from one server to the other. They are not going to pass through your computer. The data flow traffic is going to pass from one server to the other directly.
:) Cheers 
Anwar Jamal faiz