UNDER CONSTRUCTION!!!

Tech News

Keeping You Up To Date With The Latest Tech News & Virus Threats
Font size: +

How to Transfer Files Between Systems Using scp and rsync

How to Transfer Files Between Systems Using scp and rsync

Key Takeaways

Both scp and rsync can transfer files securely, but rsync is better for regularly scheduled transfers and handling network issues. Use scp for manual file transfers; rsync is more suited for scripts and cron jobs with advanced options like compression. Both require SSH for security, but rsync also needs to be installed on both computers to be used.

There are plenty of tools for transferring files between two computers on the Linux command line, but the scp and rsync commands will cover almost any use case. Here’s how and when to use them.

What is the Difference Between scp and rsync?

Both the scp and rsync commands move files between two computers, and both will can do so very securely. The main difference between the two commands comes not in what they do, but instead in how they do it.

The scp command stands for “secure copy,” and in many ways, it works like the standard unix cp or “copy” command, but between two computers. The “secure” in the name in this case refers to SSH, the protocol it uses to communicate with the remote computer.

This command is as simple as it sounds, reading files from one computer and writing them to another. This simplicity fits well with the UNIX philosophy of “do one thing well.”

The rsync command also does one thing well, but it takes a more nuanced approach. The difference comes down to the name. While rsync will copy a file to another computer if that file isn’t already present, any future operation will only compare differences between the files on each computer.

If you’re only moving one small file, this difference won’t be very noticeable. That said, if you’re using rsync as part of a backup script, operating on only the files that have changed can save a lot of time and bandwidth. Of course, this does require rsync to be installed on both the local and remote computer, while the scp command only needs to be installed on the computer running the command.

Rsync's output over SSH looks the same as any other output.

This isn’t the only difference. The rsync command is also somewhat more resilient, and can reestablish a connection if it drops in the middle of a transfer. Combined with the ability to transfer only differences, this makes it much better suited to larger transfers.

That said, rsync also has similarities to scp. For example, it can also connect via SSH and you can use an SSH key to connect to a remote machine with either command.

How to Transfer Files With scp

Using the scp tool to move single files between computers is simple. The command can be as basic as the following:

scp <filename> username@remotehost:/path/to/directory

To copy the file “foo.txt” to a remote computer, you might use the command below:

scp foo.txt kris@some-computer:/home/kris/

To use the scp command to copy that file back to my own computer (for some reason), I could use the following:

scp kris@some-computer:/home/kris/foo.txt

Of course, in many cases, you’ll want to move entire directories of files between computers. Fortunately, this is as simple with scp as it is with the standard cp command. Just pass the -r flag to copy a directory recursively:

scp -r dotfiles kris@some-computer:/home/kris/

Or to copy a directory recursively from a remote computer, use the following:

scp -r kris@some-computer:/home/kris/dotfiles

For more in-depth info on everything you can do with this command, see our guide to using the scp command on Linux.

How to Transfer Files With rsync

While rsync has a lot of functionality built-in, it makes handling basic transfers fairly simple. The base command to copy a file to a remote computer is as follows:

rsync <local-filename> user@remotehost:<remote-filename>

So to copy the same “foo.txt” file from the scp example above, you’d use the following command:

rsync foo.txt kris@some-computer:foo.txt

In the background, rsync will try to use an SSH connection, and will ask you for your username and password. Once you’re authenticated, the transfer will begin.

rsync -r /home/dave/Documents/ /media/dave/SILVERXHD/ in a terminal window

Conversely, copying the file back to your local computer is equally simple:

rsync kris@some-computer:/home/kris/foo.txt .

Of course, operating this way isn’t really taking full advantage of the power of rsync and its various options. While rsync has plenty of flags to set various options, one you’ll want to get used to is the -a or “--archive” flag. This combines a few options that, among other things, cause rsync to recursively scan directories, preserve ownership and permissions, and copy symbolic links as symbolic links.

Given the use cases rsync is typically rolled out for, it makes sense to roll these options into one easy-to-access flag. For the vast majority of your rsync use, you’ll likely want to use the -a flag. For example, to sync a directory to a remote computer:

rsync -av ~/dotfiles kris@some-computer:/home/kris/dotfiles

The second flag in the above command is “-v” for “--verbose” which will give you more detailed output about the progress of the command as it runs.

Depending on the speed of your internet connection (and likely depending on what exactly you’re transferring) you may be able to speed up transfers by compressing the data in transit. This keeps the data uncompressed on both your local and the remote machine, but compresses the data while it’s transferred. To do this, use the -z flag:

rsync -azv ~/dotfiles kris@some-computer:/home/kris/dotfiles

Again, this isn’t guaranteed to speed up transfers, but is worth looking into, especially for frequent jobs like backup scripts or other jobs that take significant time.

This is just a glimpse of how powerful the rsync command can be. For more ideas, we have some useful examples of the rsync command in action.

When Should You Use scp or rsync?

Both the scp and rsync commands have their respective strengths and weaknesses, but in day-to-day use, determining which is best will generally come down to how you’re using them. There’s a fairly simple rule of thumb you can use to help remember which one to use in a given situation.

If you’re using the command manually to move files around, generally, you’ll have an easier time using the scp command. It’s simpler and more straightforward, and you’re probably already at least somewhat familiar with the basic flags from using the cp command.

On the other hand, if you’re writing a script or running a cron job, the rsync command will likely be your best bet. It’s better at dealing with unexpected network issues, and its more sync-driven nature means it simply has more robust options than the relatively barebones scp tool does.

Both commands are well worth knowing and both are useful to have in your tool belt of essential Linux commands. After all, you never know when you might be stuck with access to only one of the commands. In that case, it’s good to know at least the basics of both.

(Originally posted by Kris Wouk)
×
Stay Informed

When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.

Google rolls out its Gemini AI-powered video prese...
Apple Is Giving up on the Stickers
 

Comments

No comments made yet. Be the first to submit a comment
Already Registered? Login Here
Friday, 08 November 2024

Captcha Image

I Got A Virus and I Don't Know What To Do!

I Need Help!