The rsync command in Linux

rsync synchronizes directories by transferring only the differences — new files, changed blocks — which makes the second run of any backup or deploy near-instant. It works locally or over SSH, preserves metadata, can delete extraneous files, and shows progress. For moving directory trees between machines, rsync is the professional default.

How rsync works

rsync's magic is the delta algorithm. The receiver splits its version of each file into blocks and sends compact checksums; the sender slides a window over its version computing a cheap rolling checksum, and wherever it matches a receiver block (confirmed by a strong hash), it sends "you already have this block" instead of the bytes. Only genuinely new data crosses the wire — which is how a changed gigabyte file can sync in seconds.

Before any of that, rsync decides which files to examine at all using the quick check: same size and modification time means "unchanged, skip". That heuristic is why re-runs are near-instant, and also rsync's one blind spot — a file altered without changing size or mtime slips through. --checksum forces full content comparison at the cost of reading everything. Knowing both layers (file selection, then block deltas) is knowing rsync.

Syntax

rsync [OPTIONS] SOURCE DEST

Common options

OptionWhat it does
-aArchive: recursive plus preserve permissions, times, symlinks — the standard base.
-v / -PVerbose / progress with resume support.
-zCompress in transit (helps on slow links, wastes CPU on fast ones).
-nDry run: show what would happen. Use it before every new command.
--deleteRemove files from DEST that no longer exist in SOURCE (true mirroring).
--exclude=PATSkip matching paths (repeatable).
-e "ssh -p 2222"Custom remote shell/port.

How to use rsync: examples

$ rsync -avP ~/projects/ user@server:/backup/projects/

Mirror a directory to a server over SSH, with progress. Rerun tomorrow: only changes travel.

$ rsync -avn --delete src/ dest/

ALWAYS dry-run a --delete first: -n shows exactly what would be removed.

$ rsync -av --exclude="node_modules" --exclude=".git" app/ server:/var/www/app/

Deploy without the junk.

$ rsync -av user@server:/var/log/app/ ./logs/

Pull remote logs locally for analysis.

Real-world use cases for rsync

The nightly offsite backup

cron + rsync -a --delete ~/data/ backup-host:/backups/data/ is the minimum viable backup that professionals actually run: incremental, fast after the first night, and an exact mirror. Pair with --backup-dir for a daily changed-files archive and you have versioning too.

Zero-drama deploys

rsync -avz --delete --exclude=".git" build/ server:/var/www/app/ ships only changed assets, removes deleted ones, and shows exactly what moved. The same command in CI makes every deploy identical — and boring, which is the goal.

Pro tips and common mistakes

Frequently asked questions about rsync

Does the trailing slash matter?

Critically. src/ means "the contents of src"; src means "the directory itself". rsync -a src/ dest/ merges contents into dest; without the slash you get dest/src. This is the #1 rsync surprise.

rsync or scp?

rsync for directories, repeated transfers, resume and progress; scp only for tossing a single file quickly. scp is fine; rsync is better at everything except brevity.

Is --delete dangerous?

It faithfully mirrors deletions — pointed at the wrong destination it faithfully destroys it. The discipline: pair every new --delete command with -n first.

Related commands

tar

Create and extract archives: tar.gz, tar.bz2 — with the flag combinations finally explained.

ssh

Log into remote machines, run remote commands, use keys instead of passwords, and tunnel ports.

scp

The quickest way to move a file to or from a remote machine.

Want to build real fluency? The interactive course takes you through rsync and every other essential command with guided, checked exercises.