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 [email protected]_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 [email protected]:/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 [email protected]:/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 [email protected]:/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 [email protected]:/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 [email protected]:/home/susan

rsync -azvh – -progress [email protected]:/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 [email protected]:/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.

rsync –avzhe ‘ssh –p 300’ /datafile [email protected]:/home/susan

here, this example will let us use rsync over ssh with modified port.

rsync -azvh – -progress –include ‘A*’ –exclude ‘*’ [email protected]:/home/susan /datafile

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.

Don’t forget to leave your comments/queries in the comment box. ADIOS!!!

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Donate us some of you hard earned money:

Linux TechLab is thankful for your continued support.

Shusain

Passionate about Linux & open source. Loves to learn, read & write about Linux as well as new technologies.

You may also like...

6 Responses

  1. Philip Miller says:

    This is very good but it does not work with AWS servers. Still missing public keys. How to identify the public key on the sending and receiving servers.

  2. Jim says:

    small typo in the article when describing the options avzh

    currently says
    “here, option r” should be “here, option a”
    “Second option r” should be “Second option z”

    ==

    rsync –azvh /datafile [email protected]:/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,

  3. Srinivas says:

    Hi,
    Is there an option to use rsync command with http URL?
    I want to sync my local repo with remote maven repo.
    I have tried rsync -avhz http://example.com/mr/ds /home/.m2/repository
    but it didn’t work for me.
    Could you please help me on this?

    • Shusain says:

      With rsync, its not possible but there is another command you can use called zsync. Though i have not used it personally but i am aware that this will help you out.

  4. Srinivas says:

    Hi Shusain,
    Thank you for the suggestion.
    Zsync will do partial/differential file download.
    We have to mention the file name which we want to download in every time.
    Ex: zsync http://example.com/some/filename.zsync
    In my case I want incremental download of complete repository.
    Please correct me if I am wrong.

Leave a Reply

Your email address will not be published. Required fields are marked *