The rsync command copies files and directories, locally or over SSH to another server, and only transfers what has changed. For almost every admin task, one line does the job: rsync -a source/ destination/. What makes rsync dangerous are small details that are easy to miss: a missing trailing slash, swapped arguments, an empty variable, or a disk that happens not to be mounted. Add --delete, and those small details turn into deleted data.
On 2 December 2026 we measured what rsync actually does in these cases on our development server (Ubuntu 24.04.5, rsync 3.2.8). For the SSH tests, the remote side was a throwaway container running Alpine 3.24 with rsync 3.5.2, reachable only on 127.0.0.1. All test data lived in /tmp and has been deleted. Three results up front:
1. If the source is empty, for example because a USB disk was not mounted,
rsync -a --deletewipes the entire destination and returns exit code 0. In our test, 50 out of 50 files were gone without a single warning.2. With an empty variable in the destination path (
"$SERVER:$DEST/"withDESTempty), rsync targets the server’s root directory. Our dry run would have deleted 44,396 entries, includingetc/,usr/,bin/androot/.3. By default, rsync detects changes by size and modification time only. A file with changed content but identical size and timestamp stayed wrong on the destination, and rsync reported 0 files transferred. Only
-ccaught it.
Video: “How to Use rsync: Linux File Sync Tutorial (No Data Loss)” by IT CONTENTS, August 2025. A little older, but the basics haven’t changed since.
The rsync command in 60 seconds
If you just need the right line quickly:
# Copy a directory's contents locally (permissions, times, links preserved)
rsync -a /source/ /destination/
# Same thing to another server over SSH
rsync -a /source/ user@server:/destination/
# With overall progress and readable sizes
rsync -a --info=progress2 -h /source/ user@server:/destination/
# Mirror: delete on the destination what's missing in the source (DRY RUN FIRST!)
rsync -a --delete --dry-run --itemize-changes /source/ /destination/
rsync -a --delete /source/ /destination/
# Different SSH port or a specific key
rsync -a -e "ssh -p 2222 -i ~/.ssh/backup_key" /source/ user@server:/destination/
The most important rule is in the slashes: source/ with a trailing slash copies the directory’s contents, source without it copies the directory itself into the destination. More on that in a moment, because this is where most rsync accidents start.
What is rsync and what is the rsync command used for?
rsync is a command-line tool for synchronizing files. It ships with practically every Linux distribution and with macOS (in an older or a different implementation), and it works over SSH without any service on the target server. The remote machine only needs the rsync binary itself, because rsync starts its counterpart there through SSH.
The difference from cp or scp is the comparison. On the second run, rsync checks what already exists on the destination and only transfers files that are new or changed. For large files it goes one step further and sends only the changed blocks over the network. That makes rsync the standard tool for:
- deploying static websites (this blog is shipped exactly like that, with
rsync -a --deletefrom the build folder) - moving data from one server to another
- nightly copies to a second disk or a backup server
- syncing large directories where little changes each day
What rsync is not is a backup program in the strict sense. A mirror with --delete replicates mistakes as reliably as good data. In our article on the 3-2-1 backup strategy we reproduced how a nightly rsync mirror dutifully copied 20 out of 20 encrypted files to the target. How to get versions out of rsync anyway is covered further down under --link-dest.
The syntax: source, destination and options
The structure is always the same:
rsync [OPTIONS] SOURCE [SOURCE ...] DESTINATION
Source and destination can be local paths or remote ones in the form user@server:/path. Only one side can be remote. rsync can’t copy directly from server A to server B while you sit on machine C. For that, you log in to A and run rsync there.
The order is fixed: source first, then destination, just like cp. That sounds trivial, but combined with --delete a swap is fatal. We tried it: a src directory with 5 current files and a backup directory with 50 old ones. With swapped arguments, rsync -a --delete backup/ src/, the src directory afterwards contained the 50 old files, and the 5 current ones were deleted. rsync did exactly what the line said.
The most important options of the rsync command
| Option | Meaning | When you need it |
|---|---|---|
-a | Archive mode: recursive, permissions, times, symlinks, group, owner, devices | Almost always |
-v | Verbose: list each transferred file | While trying things out |
-z | Compress data over the network | Text-heavy data over slow links |
-h | Human-readable sizes | With --stats or progress |
-n / --dry-run | Trial run, nothing is changed | Before every --delete |
-i / --itemize-changes | Show what happens to each file | Together with -n |
--delete | Delete files on the destination that are missing in the source | Real mirrors |
-H | Preserve hard links | Backups, snapshot folders, package caches |
-A / -X | Preserve ACLs / extended attributes | System migrations, SELinux |
-S | Handle sparse files efficiently | VM images, database files |
-c | Compare checksums instead of size+time | When you need certainty |
-P | --partial --progress | Large files over flaky links |
-e | Set the remote shell, e.g. ssh -p 2222 | Non-default SSH port, specific key |
--exclude | Exclude paths | node_modules, caches, logs |
--bwlimit | Limit bandwidth | Don’t saturate a production server |
--mkpath | Create missing parent directories on the destination | Dated target folders |
So the famous rsync -avz means: archive mode, verbose output, compression. For local copies -z does nothing useful, and the same goes for already compressed data (images, videos, ZIPs). Our measurement on that follows below.
Why -a instead of -r
-r only copies recursively and drops timestamps and permissions. That has a side effect that’s easy to overlook: on the next run, the files on the destination look “newer” or “different” from those in the source. We copied 1,000 files twice in a row. With -r, rsync transferred 720 of 1,000 files again on the second run, although nothing had changed. With -a, the second run transferred 0.
-a as a regular user
-a includes -o (owner) and -g (group). As a regular user you aren’t allowed to set foreign owners, though. rsync doesn’t abort in that case. It silently writes your own user instead. In our test the source file belonged to UID 1234, and on the destination it ended up as 65534 (the user rsync ran as), with exit code 0. For a server migration, that means: to keep owners correct, rsync has to run as root on the receiving side (or use --fake-super).

The trailing slash: the key difference in the rsync command
With rsync, a trailing slash on the source decides the result. Our test with the identical source src/a/x:
rsync -a src dst1/ # result: dst1/src/a/x
rsync -a src/ dst2/ # result: dst2/a/x
Without the slash, the src directory ends up inside the destination. With the slash, its contents end up in the destination. A way to remember it: the trailing slash means “whatever is inside this directory”.
On the destination, the slash doesn’t matter. rsync -a src ts/d1 and rsync -a src ts/d2/ both created src/a in their target directories in our test.
Why this gets dangerous with —delete
Combined with --delete, a missing slash means syncing against the wrong level. We prepared a destination with 50 old files and a source with 5 new ones:
rsync -a --delete src backup/(no slash): nothing was deleted. The destination afterwards held 51 entries, the 50 old files plus a newsrcsubdirectory. The mirror was silently broken, and the disk fills up a bit more with every run.rsync -a --delete src/ backup/(with slash): the dry run listed 50*deletinglines, and the real run left only the 5 new files. That’s how it’s meant to work.
A dry run would have shown the difference immediately in both cases: 0 deletions where you expect 50, or 50 where you expect 0. That’s why it belongs in front of every --delete.
—delete without data loss: the dry run
--delete turns a copy into a mirror: anything that doesn’t exist in the source gets removed from the destination. That’s what you want when you ship a built website and old pages should disappear. It’s a disaster when the source or destination isn’t what you think it is.
The safe sequence:
# 1. Dry run, show deletions only
rsync -a --delete --dry-run --itemize-changes source/ dest/ | grep '^\*deleting'
# 2. Read the list. Anything that should stay? Then STOP.
# 3. Only now for real
rsync -a --delete source/ dest/
--itemize-changes (short -i) prints a code per file. >f+++++++++ means “new file gets transferred”, *deleting means “will be deleted”. Chaining the dry run and the real run in the same command (with &&, for example) defeats the purpose. The point is for a human to read the list before anything is deleted. We learned that the hard way: dry run and real sync in a single line, and a folder in the web root that wasn’t part of the build was gone. Since then, our deploy routine has an --exclude for that folder and a pause to read.

The empty source: exit code 0, everything gone
The most dangerous case doesn’t even need a typo. A nightly script mirrors /mnt/usb/ to a backup server. One night the USB disk isn’t mounted. /mnt/usb/ still exists, as an empty directory.
We reproduced exactly that: an empty directory as the source, 50 files on the destination, rsync -a --delete mnt/ backup/. Result: exit code 0, 0 files on the destination. From rsync’s point of view this was a successful sync: the source is empty, so now the destination is too.
rsync behaves differently when the source doesn’t exist at all. Then it aborts with “No such file or directory” and exit code 23, and skips deletions. In our test, all 50 files survived. So rsync disables the delete phase on I/O errors by itself, but that doesn’t help against an empty directory.
Three safeguards we tested:
# 1. Check it's really mounted
mountpoint -q /mnt/usb || { echo "disk missing"; exit 1; }
# 2. Cap the number of deletions
rsync -a --delete --max-delete=100 /mnt/usb/ backup/
# 3. A marker file that only exists on the real disk
test -f /mnt/usb/.this-is-the-real-disk || exit 1
In our test, --max-delete stopped after 10 of 50 deletions (“Deletions stopped due to —max-delete limit (40 skipped)”) and exited with code 25. So 10 files were still gone. The limit prevents total loss, but it doesn’t replace the check beforehand.
The empty variable: rsync against the server’s /
In scripts, the destination path often lives in a variable: rsync -a --delete data/ "$SERVER:$DEST/". If DEST is empty because a config file wasn’t loaded, that becomes server:/. We ran this against our throwaway container as a dry run only: rsync would have deleted 44,396 entries, including the complete etc/, usr/, bin/ and root/ directories.
One line of Bash prevents it:
rsync -a --delete data/ "$SERVER:${DEST:?destination missing}/"
${DEST:?…} aborts if the variable is empty or unset. In our test this ended with exit code 127 and the message “DEST: destination missing”, and rsync never started. set -u at the top of a script additionally catches variables that were never set, but not ones set to an empty string. If you only use one of the two, use :?.
—delete and excluded files
Anything excluded with --exclude is left alone by --delete on the destination. In our test, an old node_modules folder stayed on the destination even though it was missing from the source. Only --delete-excluded removed it. That’s usually intended (for .env files that only live on the server, for example), but it can also explain why a mirror grows larger than its source over time.
Copying files to another server over SSH
For remote paths, rsync uses SSH automatically. Anything that works with ssh user@server works with rsync. How SSH keys work is covered in What is SSH?.
# Upload
rsync -a ./website/ deploy@server:/var/www/website/
# Download
rsync -a deploy@server:/var/log/nginx/ ./logs/
# Different port, specific key
rsync -a -e "ssh -p 2222 -i ~/.ssh/deploy" ./website/ deploy@server:/var/www/website/
In scripts you can also set the SSH options via the RSYNC_RSH environment variable, so you don’t have to put -e into every call. rsync must be installed on the target server. If it’s missing, you get “rsync: command not found” or “connection unexpectedly closed”. The fix is apt install rsync on the target.
Missing target directories and —mkpath
rsync creates the last directory in the destination path, but no missing parents. In our test, rsync -a src/ snap/2026-12-01/ aborted with “mkdir … failed: No such file or directory” and exit code 11, because snap/ didn’t exist yet. Since rsync 3.2.3 there’s --mkpath for that, and the same command went through. On older systems, create the directory with mkdir -p first (over SSH: ssh server mkdir -p /path).
How rsync detects what changed
Before rsync transfers a file, a quick check decides whether it’s different at all. By default, rsync only compares size and modification time. If both match, the file counts as unchanged without a single byte being read. That’s what makes rsync fast, but it leaves a gap.
We changed a single byte in a 10 KB file and then restored the old timestamp. Same size, same time, different content. rsync -a reported 0 files transferred, and cmp showed that source and destination differed. With rsync -ac (checksums), the file was detected and transferred.
That sounds contrived, but it happens in practice: with programs that deliberately preserve timestamps, or with very quick changes within the same second. It actually happened to us by accident in another test: a file a containing “alt” on the destination, “neu” written to the source within the same second, both 4 bytes. rsync skipped it. Only with a one-second gap was it transferred.
When -c makes sense: verifying after a migration, for critical configuration, or when you don’t trust a destination. The price is that rsync reads every file completely on both sides. For 1,000 small files that took 0.06 instead of 0.04 seconds in our test. For terabytes it takes hours.
Delta transfer: only changed blocks
For files that did change, rsync doesn’t send the whole file over the network, only the blocks that differ. We measured this with a 200 MB file over SSH:
| Run | New data sent | Reused | Total sent | Time |
|---|---|---|---|---|
| First run | 209.7 MB | 0 | 209.8 MB | 1.28 s |
| 1 MB changed in the middle | 1.06 MB | 208.7 MB | 1.1 MB | 0.70 s |
Another 1 MB changed, with -W | 209.7 MB | 0 | 209.8 MB | 1.42 s |
After changing 1 MB in a 200 MB file, rsync sent a little over 1 MB instead of 200. With -W (--whole-file), the algorithm is switched off and everything goes over the wire again.
Two caveats: for local copies, rsync uses --whole-file by default, because reading both files there would cost more than simply rewriting. And the time saved depends on the link. Our test ran over localhost, where the network is hardly the bottleneck, and rsync still has to read and reassemble the whole destination file. Over a real DSL or mobile connection, though, the difference between 1 MB and 200 MB is dramatic.

Compression with -z: when it’s worth it
-z compresses data before sending it. Between our two rsync versions (3.2.8 and 3.5.2), rsync automatically negotiated zstd (level 3) for compression and xxh128 for checksums. You can see that with --debug=nstr.
- 58 MB of log files (real syslog excerpts, 30 parts): without
-z, 60.0 MB went over the wire, with-zit was 6.4 MB. For comparison,gzip -6shrank the same data to 7.1 MB. - 200 MB of random data (behaves like videos, JPEGs, ZIPs): with
-z, 206.6 MB, so no gain. Time was practically the same at 1.27 vs. 1.34 seconds.
For logs, database dumps, source code and HTML, -z almost always pays off over the internet. For photo archives and already compressed backups, you can leave it out. rsync also has a built-in list of extensions (such as jpg, mp4, zip, gz) that it won’t recompress with -z anyway.
rsync vs. scp: many small files
For a single file, scp and rsync are roughly equally fast. With many small files, things look different. We copied 10,000 small text files to the container:
| Command | First run | Second run (nothing changed) |
|---|---|---|
scp -r | 6.71 s | 7.08 s |
rsync -a | 1.62 s | 0.26 s |
scp handles every file individually and copies everything again the second time. rsync batches the transfer and notices on the second run that there’s nothing to do. For deployments where only a few files change, that’s the real reason to use rsync.
Excluding files and directories with —exclude
Exclude patterns are relative to the transfer root, meaning the contents of the source. That leads to surprises. Our test directory contained node_modules/ at the top level, app/node_modules/ one level down, and logs/1.log:
rsync -a --exclude '/node_modules' src/ d1/
# excludes only the top-level node_modules, app/node_modules is copied
rsync -a --exclude 'node_modules' src/ d2/
# excludes node_modules at every level
rsync -a --exclude 'node_modules/' --exclude '*.log' src/ d3/
# only directories named node_modules, plus all .log files
rsync -a --exclude 'src/node_modules' src/ d4/
# excludes NOTHING, because "src" isn't part of the transferred path
The last one is the classic: you write the path as you see it in the terminal, but rsync only sees what’s below src/. The pattern never matches, and there’s no warning. A dry run with -n -v shows whether your pattern works.
For longer lists there’s --exclude-from=file, one pattern per line. If you only want to copy specific things, combine --include and --exclude. Order matters: the first matching rule wins.
Hard links and sparse files: what -a leaves out
-a sounds like “all”, but it leaves out a few things: hard links (-H), ACLs (-A), extended attributes (-X) and sparse files (-S). We measured two of them:
- Hard links: a 50 MB file with a second hard link uses 51 MB in the source. Copied with
rsync -a: 101 MB on the destination, because the two names became two real copies. Withrsync -aH: 51 MB. - Sparse files: a 1 GB file made almost entirely of holes (typical for VM images) uses 4 KB in the source. With
rsync -a: 1.1 GB on disk. Withrsync -aS: 4 KB.
For a complete server migration, use rsync -aHAXS. If the destination is fuller than the source afterwards, one of these two effects is the first suspect. How to track that down is covered in our article Linux disk full.
Resuming interrupted transfers
When a large transfer is interrupted, rsync deletes the half-finished file on the destination by default. We aborted a 200 MB transfer after half a second: without extra options, the destination directory was empty afterwards. With --partial, 9.96 MB stayed on the destination as a partial file, and the next run reused exactly those 9.96 MB and only transferred the rest.
-P is short for --partial --progress. For large files over unreliable links, that’s the right choice. With --partial-dir=.rsync-partial, partial files go into a separate folder, so there’s never a half file under the real name on the destination. That matters when another process uses the destination files directly.
Bandwidth is capped with --bwlimit. With --bwlimit=20m, our 200 MB file took 10.17 seconds, almost exactly 20 MB per second.
Versions instead of a mirror: snapshots with —link-dest
A mirror only knows the current state. With --link-dest, though, rsync can create a separate folder for each run without storing everything again. Unchanged files are created as hard links to the previous state, and only changed files take up new space.
TODAY=$(date +%F)
YESTERDAY=$(date -d yesterday +%F)
rsync -a --delete --link-dest=/backup/$YESTERDAY /data/ /backup/$TODAY/
Our test: 200 files totaling 97 MB, first state copied in full. Then one file changed, one new file added, second state with --link-dest. Both states together used 97 MB, not 194. The unchanged file f5 had 2 hard links in the second state, the changed f1 only one.
We ran into two traps ourselves:
1. Relative path, no error. A relative --link-dest path is resolved relative to the destination directory, not the current directory. With --link-dest=snap/2026-12-01, rsync looked in snap/2026-12-03/snap/2026-12-01, printed “—link-dest arg does not exist”, but exited with code 0 and copied everything again. Space used afterwards: 193 MB instead of 97. Use absolute paths.
2. Hard links are the same file. If you edit a file in one snapshot folder, you change it in every snapshot that points to the same file. In our test we appended a line to f5 in the third state, and afterwards it was changed in the first state too. Snapshot folders are read-only by convention. Real immutability only comes from the backup server protecting the states (via filesystem snapshots or append-only access, for example).

For serious backups with encryption, deduplication and restore tests, we now use restic. The comparison is in our 3-2-1 backup strategy article. For simple, browsable daily states without extra software, --link-dest is still a good solution.
Keeping old versions with —backup-dir
A lighter variant: --backup --backup-dir=PATH moves files that would be overwritten or deleted into a separate folder. In our test, the overwritten old version of a and the deleted file b ended up in _alt/2026-12-02/, and the destination held the new version. Here too, a relative --backup-dir is resolved relative to the destination.
Automating rsync: cron, systemd and exit codes
rsync belongs in scripts, and scripts need to detect errors. The exit codes we saw during our tests:
| Code | Meaning | Seen with |
|---|---|---|
| 0 | Success | also with an empty source and —delete |
| 11 | File I/O error | missing parent directory on destination |
| 23 | Partial transfer | source doesn’t exist |
| 24 | Files vanished | file deleted during transfer |
| 25 | --max-delete reached | deletion limit |
| 255 | SSH/connection error | interrupted transfer |
Code 24 is normal on live systems, for example when a log or temp file disappears between listing and copying. In our test, “file has vanished” came as a warning. Many backup scripts therefore treat 24 as success. Code 0, on the other hand, only means rsync carried out its instructions, not that the instructions made sense. No exit code can replace the checks before the run (mounted? variable set? marker present?).
A skeleton that catches the traps from this article:
#!/usr/bin/env bash
set -euo pipefail
SOURCE=/mnt/data
DEST="backup@server:/backup/data"
mountpoint -q "$SOURCE" || { echo "source not mounted"; exit 1; }
test -f "$SOURCE/.backup-marker" || { echo "marker missing"; exit 1; }
rsync -aHAX --delete --max-delete=500 \
--exclude-from=/etc/backup-exclude.txt \
"$SOURCE/" "${DEST:?destination missing}/"
For scheduling, you can use a classic cron job (see Create a cron job) or a systemd timer with its own service (see Create a systemd service). The timer has the advantage that output and exit code end up in the journal, and Persistent=true catches up on missed runs. Either way, a failed run has to land somewhere a human reads it. Otherwise you only notice at restore time.
Common error messages
| Message | Cause | Fix |
|---|---|---|
rsync: command not found / connection unexpectedly closed | rsync missing on the remote | apt install rsync on the remote |
Permission denied (publickey) | SSH key not accepted | test ssh user@server first, then -e "ssh -i …" |
mkdir … failed: No such file or directory (code 11) | parent directory missing on destination | --mkpath or mkdir -p first |
change_dir … failed (code 23) | source doesn’t exist | check the path for typos |
file has vanished (code 24) | file deleted during the run | normal for logs/temp |
--link-dest arg does not exist | relative path resolved wrongly | use an absolute path |
Deletions stopped due to --max-delete limit (code 25) | deletion limit reached | review the list, find the cause |
Frequently asked questions about the rsync command
What does the rsync command do?
rsync syncs files and directories between two locations, locally or over SSH. The first run copies everything, every subsequent run only what’s new or changed. With --delete it also removes from the destination whatever is missing in the source, creating a mirror.
What does rsync -avz mean?
-a is archive mode (recursive, permissions, timestamps, symlinks, owner), -v lists every transferred file, -z compresses the transfer. For local copies and already compressed data, -z is pointless. With our log files it cut the transferred amount from 60 to 6.4 MB.
What’s the difference between rsync and scp?
scp always copies everything, rsync only the differences. With 10,000 small files, rsync took 1.6 instead of 6.7 seconds in our test, and 0.26 seconds on the second run, while scp transferred everything again. rsync can also resume interrupted transfers, exclude, delete and preserve permissions.
What does the trailing slash on the source do?
With a slash (source/), rsync copies the directory’s contents. Without it (source), it copies the directory itself into the destination. On the destination, the slash doesn’t matter. Combined with --delete, a forgotten slash means nothing gets deleted and the mirror is silently wrong.
How do I copy to another server with rsync?
rsync -a /local/ user@server:/destination/. rsync uses SSH for this, and the server only needs rsync installed. For a different port or key: -e "ssh -p 2222 -i ~/.ssh/key".
Is rsync a backup?
An rsync --delete mirror isn’t a backup, because it immediately replicates deletions, ransomware encryption and accidental changes. With --link-dest and dated folders you get versions, and with --backup-dir at least the last old version. For encrypted, deduplicated backups, restic or Borg is the better choice.
How do I do a dry run with rsync?
Use -n or --dry-run, ideally together with --itemize-changes. Pipe it through grep '^\*deleting' to see only the planned deletions. Read the list before starting the real run.
Why doesn’t rsync —delete delete anything?
Usually the trailing slash on the source is missing, so rsync syncs a subdirectory instead of the contents. Other causes: the files are excluded via --exclude (then --delete-excluded helps), or there was a read error, in which case rsync deliberately skips the delete phase.
Can rsync wipe the entire destination?
Yes. If the source is an empty directory, such as an unmounted mount point, rsync --delete deletes everything on the destination and reports success. Check with mountpoint -q or a marker file beforehand, and set --max-delete as an emergency brake.
How do I show progress in rsync?
--progress shows progress per file, --info=progress2 shows overall progress across all files. Add -h for human-readable sizes.
How do I exclude directories?
With --exclude 'pattern'. Patterns without a leading slash apply at every level, with a leading slash only at the top. Paths are relative to the source contents: --exclude 'src/node_modules' won’t match when you transfer src/.
Does rsync detect every change?
Not always. By default, rsync only compares size and modification time. A file with the same timestamp and size but different content stayed wrong on the destination in our test. With -c, rsync compares checksums and catches it, at the cost of reading every file completely.
Conclusion: the rsync command is simple, the safeguards make the difference
For everyday work you only need a handful of options: -a, the right trailing slash, -e for SSH and --exclude. With those, rsync replaces cp and scp almost everywhere and is many times faster on repeated copies.
The danger lies in --delete, and our tests show why: neither an empty source, nor an empty variable, nor a wrong --link-dest path produces a failing exit code. rsync does what the line says and reports success. The safeguards therefore have to happen before rsync runs: read the dry run, check the mount point, protect variables with :?, set --max-delete as an emergency brake.
If you take only one habit from this article: before every --delete, run it once with -n -i and read the *deleting lines. It takes ten seconds.
A compact overview of rsync and the other 59 commands you’ll actually need when starting out is in our Linux commands list.
