How to Use the Linux rsync Command for File Transfers

The File Transfer Nightmare

If you need to copy a small 5MB configuration file from one Linux server to another, the standard scp (Secure Copy) command works perfectly. However, if you need to migrate an entire 50GB website directory containing a million tiny image files across the internet, scp will fail you.

Why? Because network connections drop. If you are 45GB into a 50GB transfer using scp and your Wi-Fi blinks for two seconds, the transfer dies. When you restart the command, scp is not smart enough to resume where it left off. It will overwrite everything and start from byte zero, wasting hours of bandwidth.

Professional system administrators do not use scp for large data migrations. They use rsync (Remote Sync). It is a brilliantly engineered tool that compares the source and destination folders, identifies exactly what is missing, and only transfers the delta (the differences). If a transfer drops, rsync picks up exactly where it left off.

The Basic Rsync Syntax

The syntax for rsync looks very similar to the standard cp (copy) command. You state what you want to copy (the source), and where you want it to go (the destination).

To copy a folder locally on the same server:

rsync -av /var/www/html /mnt/backups/

Notice the -av flag. You should use this on almost every rsync command you write:

  • -a (Archive): This tells rsync to perfectly preserve all the metadata of the files, including who owns them, the read/write permissions (chmod), and the timestamps. Without this, your copied website might break because the file permissions changed during transit.
  • -v (Verbose): This prints a running list of every file as it is being copied to your terminal, so you aren’t just staring at a blank screen wondering if it crashed.

The Trailing Slash Rule (CRITICAL)

The most common mistake beginners make with rsync is misunderstanding the trailing slash on the source folder. It completely changes the behavior of the command.

  • Scenario A (No Slash): rsync -av /var/www/html /mnt/backups/
    This command takes the entire “html” folder itself and places it inside the backups folder. Result: /mnt/backups/html/
  • Scenario B (With Slash): rsync -av /var/www/html/ /mnt/backups/
    The trailing slash means “the contents of this folder.” It takes all the files inside the html folder and dumps them directly into the backups folder, without creating a master “html” folder.

Always double-check your slashes before pressing Enter.

Remote Transfers (Server to Server)

Rsync uses standard SSH to securely transfer files across the internet. The syntax simply adds the remote server’s login credentials to the destination path.

To push a local folder to a remote server:

rsync -av /var/www/html [email protected]:/var/www/backup/

Rsync will connect to the server at 192.168.1.50 via SSH, ask for the password (or use your SSH keys), and securely stream the files.

The Resume Feature: If you run this exact command, let it run for 10 minutes, and then pull your ethernet cable out, the transfer will die. If you plug the cable back in and run the exact same command a second time, rsync will quickly scan both sides, realize the first 10,000 files are already there, and immediately skip to file 10,001. It is flawless.

The Delete Flag (True Synchronization)

By default, rsync only adds files; it never deletes them. If you delete an image on Server A and run rsync to back it up to Server B, that image will still exist on Server B. It is an additive copy.

If you want Server B to be a perfect, exact mirror of Server A, you must tell rsync to delete any files on the destination that no longer exist on the source. You do this with the --delete flag.

rsync -av --delete /var/www/html/ [email protected]:/var/www/backup/

Warning: Use this flag with extreme caution. If you accidentally point the destination to the wrong folder, rsync will ruthlessly delete everything in that folder to match your source.

Conclusion

For any file transfer larger than a few megabytes, rsync is the only tool you should trust. By mastering the -a (Archive) flag for permissions and the --delete flag for true mirroring, you can execute massive server migrations and bulletproof daily backups with absolute confidence.

Related posts

  1. Essential Linux Terminal Keyboard Shortcuts to Work Faster
  2. How to Find Large Files and Directories in Linux
  3. How to Create and Extract ZIP Files in the Linux Terminal

Leave a Reply

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

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.