How to move files between systems using scp and rsync
There are many tools for moving files between two computers using the Linux command line, but the scp and rsync commands are two of the most popular options, covering almost every use case. This article will show you how and when to use the scp and rsync commands in a Linux environment.
What is the difference between scp and rsync?
Both scp and rsync can support moving files between two computers, and both can do so very securely. The main difference between the two commands is not their purpose, but how they do the job.
The scp command stands for " secure copy " and in many ways it works just like the standard unix cp or " copy " command, but between two computers. " secure " in this case refers to SSH, the protocol the command uses to communicate with the remote computer.
This command is as simple as its name suggests, reading files from one computer and writing them to another. This simplicity fits the UNIX philosophy of "doing one thing well".
The rsync command does a similar job, but takes a more subtle approach. The difference is in the name. rsync will copy a file to the other computer if it doesn't already exist, any future operations will just compare the differences between the files on each computer.
If you're only moving a small number of files, this difference won't be very noticeable. However, if you're using rsync as part of a backup script, running it on files that have changed can save a lot of time and bandwidth. Of course, this requires rsync to be installed on both the local and remote computers, whereas the scp command only needs to be installed on the computer running the command.
This isn't the only difference. The rsync command is also more 'durable' and can re-establish a connection if it drops mid-transfer. Combined with the ability to only transfer the data that differs, this makes it much more suitable for tasks that require transferring large amounts of data.
rsync also has similarities with scp. For example, it can also connect over SSH and you can use SSH keys to connect to the remote machine with any command.
How to transfer files using scp
Using the scp tool to move single files between computers is very simple. The command can be as basic as:
scp username@remotehost:/path/to/directory
To copy the file ' foo.txt ' to the remote computer, you can use the command below:
scp foo.txt kris@some-computer:/home/kris/
To use the scp command to copy that file back to your computer (for whatever reason), you can use the following command:
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 command:
scp -r kris@some-computer:/home/kris/dotfiles
How to transfer files using rsync
rsync has a lot of built-in functionality that makes handling basic tasks fairly simple. The basic command to copy files to a remote computer is as follows:
rsync < local-filename > user@remotehost:< remote-filename >
To copy the same file 'foo.txt' from the scp example above, you would use the following command:
rsync foo.txt kris@some-computer:foo.txt
In the background, rsync will attempt to use an SSH connection and will ask you for a username and password. Once you are authenticated, the transfer will begin.
Conversely, copying the file back to your local computer is just as simple:
rsync kris@some-computer:/home/kris/foo.txt .
Of course, doing it this way doesn't really take advantage of the full power of rsync and its various options. rsync has many flags to set different options, but one flag you'll want to use is -a or "--archive". Combining a few options, rsync can recursively scan directories, preserve ownership and permissions, and copy symbolic links as symbolic links.
Given the common use cases for rsync, it makes sense to put these options in an easily accessible flag. For most users' rsync needs, the -a flag is the usual case. For example, to synchronize a directory with a remote computer:
rsync -av ~/dotfiles kris@some-computer:/home/kris/dotfiles
The second flag in the above command is ' -v ' -verbose '. This flag will give you more detailed information about the progress of the command as it runs.
Depending on your internet connection, you can speed up your transfer by compressing the data during transfer. This keeps the data uncompressed on both your local and remote machines, but compresses the data during transfer. To do this, use the -z flag :
rsync -azv ~/dotfiles kris@some-computer:/home/kris/dotfiles
Again, this isn't guaranteed to speed up data transfers, but it's still worth considering, especially for frequent tasks like backup scripts or other jobs that take a long time.
When to use scp or rsync?
Both the scp and rsync commands have their own strengths and weaknesses, but in everyday use, determining which command is best often depends on how you use them. There is a fairly simple rule of thumb that you can use to see which command to use in a given situation.
If you're using a manual command to move files, it's generally better to use scp. It's simpler and easier to understand, and you're probably already at least familiar with the basic flags used with the cp command.
On the other hand, if you're writing a script, rsync might be the best choice. It's better at handling unexpected network failures, and the deeper nature of the sync means it has more powerful options than the relatively simple scp utility.
Both commands are worth knowing and are useful to have in your essential Linux command toolkit.
You should read it
May be interested
- How to add 'Move to' or 'Copy to' to the Windows 10 context menuwindows 10 has a hidden function that allows you to right-click a file and move or copy it to a specific location desired. this small registry tip will help you copy and move files easily.
- How to Play VOB Filesyou can play most vob files using vlc player, which is available for all major operating systems. you can also use mpc-hc in windows, which functions very similarly. if you have a plex media server, converting the vob files to mkv format...
- How to Move Files from One PC User to Anotherif a particular computer has multiple users, as in the case with workplaces, moving files between user accounts may be required. it may seem a difficult and time-consuming process, but actually it's not; it's quick and easy to do, for both...
- How to use Zsync to transfer a file part in Linuxzsync is based on rsync, another popular linux tool to synchronize files or directories, and it's very easy to use. most linux distributions already have zsync available in the package repository, so it's easy to install and get started.
- Instructions on how to share data between Windows and Linux via LANyou tried using a flash drive or sd card to transfer files from linux to a windows computer and found it annoying, especially when there are too many files to move and will take a long time to complete. .
- Instructions on how to move tables in Wordthe following article introduces you to move tables in word. before you move you need to edit the properties for the table otherwise move or copy the table is not in the right position.
- How to extract IMG files in Windows 10anyone who has burned a disc is probably familiar with most disk image file formats. img is one of them and they are commonly used to compress programs, such as operating systems, software or video games.
- How to Save Files to USBthis article shows you how to move, save, and download files to a usb attached to your computer.
- 4 Ways to copy files faster in Windowscopy feature in windows 7 is nothing outstanding, it is only temporary, it is not the fastest when you proceed to copy and move one or more large files. however, there are several ways to help you quickly copy files efficiently in windows.
- Instructions for opening Zip files on Android and iOSalthough the most popular compressed file format in the world today, zip files do not work well on smart phones. when trying to open a zip file on ios and android operating systems, you will often receive an error message. however, with an appropriate third-party application, problems with these zip files can be easily solved.