Files transfer using scp & rsync commands

Hello Linux-fanatics, in this tutorial we will discuss how to transfer files among system using scp & rsync with some examples. (also read tutorial on “Cloning disks using dd & cat commands“). But firstly let’s discuss both commands in brief.

scp & rsync commands

scp is based on ssh & is used to transfer file from local host to remote host securely. Syntax for using scp is

scp source_file_name username@destination_host:destination_folder

Rsync’s main function, on the other hand is basically to synchronize files/directories either on local host or on remote host. Rsync initially copies whole directory & then copies newly added files (differential backup) rather than copying whole directory again.

It is secure & faster than scp & can also be used in place of scp command to copy files/directories to remote host. Syntax for using rsync is

rsync options source destination

Now, let us discuss some examples showing uses of both commands.

Examples for scp

scp –rpv /datafile dan@192.168.1.100:/home/susan

here, option r is used when we are copying a directory with all the files inside it,

secondly option p will provide an estimate time & connection speed,

and option v will provide debugging information which can help in diagnosing a problem related to connection, authentication etc.

scp –C /datafile dan@192.168.1.100:/home/susan

option C here will compress the file on the go & will reproduce the same file when it arrives at the destination. Thus saving time consumed for copying the file.

scp –P 300 /datafile dan@192.168.1.100:/home/susan

here option –P is used to define a custom port incase we are not using default ssh port (22).

Examples for Rsync

rsync –azvh /datafile dan@192.168.1.100:/home/susan

here, option r is archive mode which allows copying of files recursively along with their file permissions, symbolic links etc,

Second option r is used to compress file data,

third option h will provide human readable outputs,

and option v will provide debugging information which can help in diagnosing a problem related to connection, authentication etc,

Also, we can mention alocal location like /home/dan in place of dan@192.168.1.100:/home/susan

rsync -azvh – -progress dan@192.168.1.100:/home/susan /datafile

above example will let us synchronize a directory from remote location to local host directory & –progress will show us the progress of file/directory transfer.

rsync –avzhe ssh /datafile dan@192.168.1.100:/home/susan

this example will let us use rsync over ssh and option –e here is used to define a protocol , which is this case is ssh.

this will let us copy all files starting with “A” & will exclude all other files.

Both the scp & rsync can be used to transfer files/directories but rsync fares a little better when comes to performance. Also rysnc has option to take differential backup which scp lacks. But both are equally secure & very easy to use.