How to Use rsync for Efficient File Synchronization

rsync efficiently synchronizes files and directories, transferring only the differences between source and destination rather than copying everything every time — a core tool for backups, deployments, and file transfers.

Why rsync Over Simple cp or scp

rsync's delta-transfer algorithm only sends the parts of files that actually changed — dramatically faster than re-copying entire files for incremental syncs of large datasets, and it can resume interrupted transfers.

Basic Local Sync

rsync -av /source/directory/ /destination/directory/

The trailing slash on the source matters: with it, the contents of the directory sync into the destination; without it, the directory itself is copied as a subdirectory of the destination.

Common Flags Explained

FlagMeaning
-aArchive mode: preserves permissions, timestamps, symlinks, and recurses into directories
-vVerbose output
-zCompress data during transfer (useful over slower network connections)
--deleteRemove files in destination that no longer exist in source (true mirroring)
-nDry run — shows what would happen without actually doing it

Syncing to a Remote Server Over SSH

rsync -avz -e ssh /local/directory/ user@remote-server:/remote/directory/

Syncing from a Remote Server to Local

rsync -avz -e ssh user@remote-server:/remote/directory/ /local/directory/

Dry Run Before a Destructive Sync

rsync -avz --delete -n /source/ /destination/

Always test with -n before running with --delete for the first time on any new sync pair — verify the files it would delete are genuinely the ones you intend.

Excluding Files/Directories

rsync -av --exclude='node_modules' --exclude='.git' /source/ /destination/

Using an Exclude File for Multiple Patterns

rsync -av --exclude-from='exclude-list.txt' /source/ /destination/

Limiting Bandwidth Usage

rsync -avz --bwlimit=5000 /source/ user@remote:/destination/

Limits transfer to 5000 KB/s, useful to avoid saturating bandwidth needed for other services during a large sync.

Showing Transfer Progress

rsync -avz --progress /source/ /destination/

Automating Regular Syncs with Cron

0 2 * * * rsync -avz --delete /var/www/ /backup/www/ >> /var/log/rsync-backup.log 2>&1

Using rsync for Efficient Backups

See How to Set Up Automated VPS Backups — rsync's incremental transfer makes it well-suited for regular backup jobs, especially when combined with hard-link-based techniques for space-efficient historical snapshots.

Common Errors

Permission denied errors during sync — verify the SSH user has appropriate read/write permissions on both source and destination paths.

Unexpected files deleted after using --delete — always dry-run first (-n); this flag is powerful and unforgiving if source/destination paths are swapped by mistake.

Continue Reading

Browse more articles in Linux Server Administration.

  • rsync tutorial, file synchronization linux, rsync ssh, rsync backup
  • 0 Kasutajad peavad seda kasulikuks
Kas see vastus oli kasulik?

Seotud artiklid

How to Update and Upgrade Your Ubuntu or Debian VPS

Keeping your VPS updated is one of the most important and simplest maintenance tasks — it...

How to Enable and Configure Swap on a Linux VPS (Ubuntu & Debian)

Swap is disk space Linux can use as overflow memory when physical RAM is exhausted. It won't make...

How to Manage Services with systemd and systemctl

systemd is the service manager used by Ubuntu, Debian, and most modern Linux distributions to...

How to Read and Analyze Linux Logs with journalctl

On modern Ubuntu and Debian systems, journalctl is the central tool for viewing system and...

How to Schedule Tasks with Cron on a Linux VPS

Cron is the standard Linux job scheduler, used to automate recurring tasks like backups, log...