File Transfers Using SCP, Rsync, and Robocopy

File Transfers Using SCP, Rsync, and Robocopy

Published
4 min read

Table of Contents

File Transfers Using SCP, Rsync, and Robocopy

When it comes to transferring files between different machines or servers, there are a few popular tools available such as SCP, Rsync, and Robocopy. While they all serve the same purpose, there are some notable differences between them.

SCP

SCP (Secure Copy) is a command-line tool used for securely transferring files between two remote systems. It uses the SSH (Secure Shell) protocol for authentication and encryption, making it a secure way to transfer files. SCP only allows for copying files and directories from the source to the destination, and it doesn’t support mirroring or syncing. This means that if you need to update the files on the destination system, you’ll need to manually copy them again.

Copy a file from the local system to a remote system:
scp file.txt user@remotehost:/remote/directory/
Copy a file from a remote system to the local system:
scp user@remotehost:/remote/directory/file.txt .
Use the -i switch for key based authentication:
scp -i ~/.ssh/key.rsa user@remotehost:/remote/directory/file.txt .
Use the -r switch to copy an entire directory:
scp -r user@remotehost:/remote/directory/ ./

RSync

Rsync is a more advanced tool that is used for syncing files between two systems. It also uses SSH for secure transfer and can move files over the network using the rsync protocol. Rsync is capable of detecting and copying only the differences between the source and destination files, which makes it faster than SCP for large files or directories. It also has the ability to resume interrupted transfers and can be used for remote backups.

Copy a file from the local system to a remote system:
rsync file.txt user@remotehost:/remote/directory/
Copy a file from a remote system to the local system:
rsync user@remotehost:/remote/directory/file.txt .
Sync a local directory with a remote directory:
rsync -avz /local/directory/ user@remotehost:/remote/directory/
  • a stands for “archive” and is used to copy files recursively, preserve symbolic links, file permissions, ownership, and timestamps.

  • v stands for “verbose” and is used to show the details of the transfer on the command line.

  • z stands for “compress” and is used to compress the data being transferred, which can be useful when transferring large files or directories over a slow network connection.

Show progress and info during file transfer:
rsync --progress --info /source/dir user@remotehost:/destination/dir
Use a SSH key:
rsync -avz -e "ssh -i ~/.ssh/key" /source/dir user@remotehost:/destination/dir

Robocopy

Robocopy is a command-line tool that comes with Windows and is used for copying and syncing files between local and remote systems. It’s a powerful tool that can be used to copy large files and directories, including those that are locked or in use. It also supports features such as multithreading and the ability to retry failed transfers.

Unlike SCP and Rsync, Robocopy doesn’t use SSH for secure transfer, but it supports other authentication methods like SMB (Server Message Block) and FTP (File Transfer Protocol).

Copy a file from the local system to a remote system:
robocopy c:\local\directory \\remotehost\remote\directory file.txt
Copy a file from a remote system to the local system:
robocopy \\remotehost\remote\directory c:\local\directory file.txt
Sync a local directory with a remote directory:
robocopy /MIR c:\local\directory \\remotehost\remote\directory

/MIR is used to mirror the contents of the source directory to the destination directory.

Here’s what it does:

  • Copies files and directories from the source to the destination, including subdirectories.

  • Deletes any files and directories in the destination that do not exist in the source.

  • Copies file attributes and timestamps, including security attributes.

  • Retries any failed copies, and skips files that are in use.

Final Thoughts

In conclusion, the choice of which tool to use depends on your specific needs. If you only need to copy a few files securely between two Linux systems, then SCP is a good choice.

If you need to sync large amounts of files between systems and want the ability to resume interrupted transfers, then Rsync is a better option. Finally, if you’re using Windows and need a tool for copying and syncing files, then Robocopy is a great choice.