An article about setting up Ghost blogging says to use scp to copy from my local machine to a remote server:

scp -r ghost-0.3 root@*your-server-ip*:~/

However, Railscast 339: Chef Solo Basics uses scp to copy in the opposite direction (from the remote server to the local machine):

 scp -r root@178.xxx.xxx.xxx:/var/chef .

In the same Railscast, when the author wants to copy files to the remote server (same direction as the first example), he uses rsync:

rsync -r . root@178.xxx.xxx.xxx:/var/chef

Why use the rsync command if scp will copy in both directions? How does scp differ from rsync?

share|improve this question
    
This is rather off-topic on SO (at least by today standards)... – Dan Cornilescu Sep 13 '16 at 5:33
up vote 219 down vote accepted

The major difference between these tools is how they copy files.

scp basically reads the source file and writes it to the destination. It performs a plain linear copy, locally, or over a network.

rsync also copies files locally or over a network. But it employs a special delta transfer algorithm and a few optimizations to make the operation a lot faster. Consider the call.

rsync A host:B
  • rsync will check files sizes and modification timestamps of both A and B, and skip any further processing if they match.

  • If the destination file B already exists, the delta transfer algorithm will make sure only differences between A and B are sent over the wire.

  • rsync will write data to a temporary file T, and then replace the destination file B with T to make the update look "atomic" to processes that might be using B.

Another difference between them concerns invocation. rsync has a plethora of command line options, allowing the user to fine tune its behavior. It supports complex filter rules, runs in batch mode, daemon mode, etc. scp has only a few switches.

In summary, use scp for your day to day tasks. Commands that you type once in a while on your interactive shell. It's simpler to use, and in those cases rsync optimizations won't help much.

For recurring tasks, like cron jobs, use rsync. As mentioned, on multiple invocations it will take advantage of data already transferred, performing very quickly and saving on resources. It is an excellent tool to keep two directories synchronized over a network.

Also, when dealing with large files, use rsync with the -P option. If the transfer is interrupted, you can resume it where it stopped by reissuing the command. See Sid Kshatriya's answer.

share|improve this answer
7  
You say to use scp for your day to day tasks. From your explanation and the OP, it appears that the interface for the two tools when used without options is identical. If rsync has the superior implementation and the interfaces are the same, why not always use rsync? – Alex May 6 '14 at 21:16
2  
Their interface is not the same, even without options. Rsync has special interpretation of a trailing slash on the source argument (synchronize the directory itself versus its contents). It might have other gotchas, I'm not sure. – Rafa May 6 '14 at 21:32
2  
Also, I would guess scp is more likely to be available on a Unix-like system, so you prevent an annoying "command not found" now and then. – Rafa May 6 '14 at 21:58
    
How does rsync handle getting killed halfway through or losing network halfway through? I often see people do scp+mv+traps to make sure that they can also behave well in error cases. If rsync can handle these cases well enough it might be a better day to day tool, even if it has some overhead, right? – erikbwork May 26 '15 at 9:03
1  
@erikb85 rsync has the -P flag which will display a progress bar and enable resuming from a partially completed transfer. digitalocean.com/community/tutorials/… – Ben Mar 4 '16 at 3:43

rysnc can be useful to run on slow and unreliable connections. So if your download aborts in the middle of a large file rysnc will be able to continue from where it left off when invoked again.

Use rsync -vP username@host:/path/to/file .

The -P option preserves partially downloaded files and also shows progress.

As usual check man rsync

share|improve this answer
3  
-P is a fantastic option, thanks. – Joshua Pinter Oct 19 '17 at 17:04

There's a distinction to me that scp is always encrypted with ssh (secure shell), while rsync isn't necessarily encrypted. More specifically, rsync doesn't perform any encryption by itself; it's still capable of using other mechanisms (ssh for example) to perform encryption.

In addition to security, encryption also has a major impact on your transfer speed, as well as the CPU overhead. (My experience is that rsync can be significantly faster than scp.)

Check out this post for when rsync has encryption on.

share|improve this answer

it's better to think in a practical context. In our team, we use rsync -aP to replace a bad cassandra host in our cluster. We can't do this with scp (slow and no progress preservation).

share|improve this answer
    
Not really bringing anything not yet mentioned in the other answers. Plus we shouldn't even answer off-topic questions. – Dan Cornilescu Sep 13 '16 at 5:34
    
@DanCornilescu this describes a use case in a real scenario, which makes it easy for others to understand the difference. I don't see any other similar answers – del bao Sep 18 '16 at 19:36
1  
See -P explained and slowness/speed-related notes in Sid's and Rafa's answers. – Dan Cornilescu Sep 18 '16 at 19:56

One major feature of rsync over scp (beside the delta algorithm) is that it automatically verifies if the transferred file has been transferred correctly. Scp will not do that, which occasionally can result in corruption when transferring larger files. So in general rsync is a copy with guarantee.

See (centos) manpage note at the end of the --checksum option description:

Note that rsync always verifies that each transferred file was correctly reconstructed on the receiving side by checking a whole-file checksum that is generated as the file is transferred, but that automatic after-the-transfer verification has nothing to do with this option’s before-the-transfer “Does this file need to be updated?” check.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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