Quick take: The rsync command efficiently syncs files by copying only what changed. The everyday recipe is rsync -avzP source/ user@host:/dest/: archive mode, verbose, compressed, with progress. Add --delete to mirror and -n to preview.

Introduction

The rsync command synchronises files and directories between two locations — locally or over SSH — transferring only the differences. This makes it dramatically faster than copying everything each time, and the standard choice for backups, deployments, and mirroring large directory trees.

This guide covers the archive mode that preserves everything, compression and progress, mirroring with --delete, and the dry-run flag that lets you preview changes before committing.

Syntax

The basic syntax of the rsync command is:

rsync [OPTIONS] SOURCE DESTINATION

Common Options and Parameters

The most useful options and parameters for the rsync command:

OptionDescription
-aArchive mode — recursive and preserves permissions, times, symlinks, owners.
-vVerbose — list files as they transfer.
-zCompress data during transfer.
-PShow progress and allow resuming partial transfers.
-hHuman-readable numbers.
-n, --dry-runPreview what would change without transferring anything.
--deleteDelete files in the destination that no longer exist in the source (mirror).
--exclude=PATSkip files matching a pattern.
-e sshUse SSH as the transport (for remote transfers).

Practical Examples

Real rsync commands you can run today:

# Sync a directory locally
rsync -avh ~/docs/ /backup/docs/
# Sync to a remote server over SSH
rsync -avzP ./site/ irfan@server:/var/www/site/
# Preview changes without copying (dry run)
rsync -avn ./src/ /backup/src/
# Mirror exactly, deleting extras in the destination
rsync -avh --delete ./src/ /backup/src/
# Exclude a directory from the sync
rsync -avz --exclude='node_modules' ./app/ server:/srv/app/
# Use a custom SSH port
rsync -avzP -e 'ssh -p 2222' ./data/ irfan@server:/data/

rsync Backup Strategies

rsync underpins many backup systems because it is fast, resumable, and preserves everything. A robust backup script combines archive mode, compression, and an exclude list, and logs what it does.

# Nightly backup, excluding caches, with a log
rsync -avz --delete \
  --exclude='cache/' --exclude='*.tmp' \
  /var/www/site/ /backup/site/ >> /var/log/backup.log 2>&1

For versioned backups, the --link-dest option creates space-efficient snapshots: unchanged files are hard-linked to the previous backup instead of copied, so each daily snapshot looks complete while only storing what changed. This is the core idea behind tools like Time Machine and many homegrown backup scripts.

Common rsync Mistakes

The trailing slash is the mistake everyone makes once. rsync -a src/ dest/ copies the contents of src into dest, while rsync -a src dest/ copies the folder src into dest, creating dest/src. Decide which you want and be consistent.

The more dangerous mistake is --delete without a preview. Because it removes anything in the destination that is absent from the source, a wrong source path can wipe a backup. Always dry-run first with -n (rsync -avn --delete …), read the planned deletions, and only then run it for real.

Tips and Best Practices

  • Mind the trailing slash: source/ copies the contents of source, while source copies the directory itself into the destination.
  • Always run with -n (dry run) before using --delete so you can confirm exactly what will be removed.
  • -avzP is the recipe to remember: archive, verbose, compressed, with progress and resume.

Final Thoughts

rsync is the most efficient way to copy, sync, and back up files in Linux, transferring only what changed and preserving every attribute. Learn -avzP, the trailing-slash rule, and the safety of --dry-run before --delete, and you have a backup and deployment tool that scales from a single folder to entire servers.

FAQ: rsync Command in Linux

What does rsync -avz mean?+

-a is archive mode (recursive, preserving permissions, times, and symlinks), -v is verbose, and -z compresses data in transit. Adding -P shows progress and enables resuming.

How do I sync files to a remote server with rsync?+

Use rsync -avzP source/ user@host:/dest/. It transfers over SSH and copies only the changed files. Add -e 'ssh -p 2222' for a custom port.

What does the trailing slash do in rsync?+

A trailing slash on the source (src/) copies its contents into the destination, while no slash (src) copies the directory itself into the destination. This is the most common rsync gotcha.

How do I make rsync delete files that no longer exist?+

Add --delete to mirror the source exactly, removing destination files that are gone from the source. Always dry-run with -n first to confirm what will be deleted.

How do I preview an rsync transfer?+

Use -n (or --dry-run) with -v: rsync -avn source/ dest/ lists what would be copied or deleted without changing anything.

Need help with Linux servers or infrastructure?

Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.

Hire Me for Support