Where to start First steps track · 15 stages
Linux Labs

rsync — file synchronization and copying

4 min rsync 3.x

Copies only the differences between trees; --delete mirrors the source into the destination, removing anything extra.

Cheat sheet

5 commands Printable version
  • rsync -avzP src/ dst/ sync (changes only, with progress)
  • rsync -avzP src/ user@host:/dst/ over SSH
  • rsync -avzn --delete src/ dst/ dry-run a mirror FIRST
  • rsync -avz --exclude='cache/' src/ dst/
  • remember the trailing / on the source

Overview

rsync copies by transferring only the differences — fast for repeated syncs and large trees. Works locally and over SSH.

bash
rsync -avz src/ dst/              # archive + compression, changes only

The -a flag (archive) means recursion plus preserving permissions/times/symlinks/owners.

Options

11

-n (dry-run) shows what would change without writing a single byte to disk — always check a command with --delete this way before removing the flag. -a does not include deletion in the destination: without --delete, rsync only adds and updates files, it never removes anything extra.

Option What it does Example
-a --archive Archive mode (rlptgoD) — recursive, preserves permissions, times, symlinks, ownership. rsync -a src/ dst/
-v --verbose Verbose output — which files were transferred. rsync -av src/ dst/
-n --dry-run Show what would happen without changing anything on disk. dangerous rsync -avn --delete src/ dst/
--delete Delete files in the destination that are missing from the source — mirrors the source exactly. dangerous rsync -av --delete src/ dst/
-z --compress Compress file data during the network transfer. rsync -avz src/ user@host:/dst/
-P Shorthand for --partial --progress — resumable transfer with a progress bar. rsync -avP src/ dst/
--exclude PATTERN Exclude files and directories matching a pattern. rsync -avz --exclude='cache/' src/ dst/
-e command Set the remote shell to use, e.g. ssh with a non-default port. rsync -avz -e 'ssh -p 2222' src/ host:/dst/
--bwlimit KBPS Limit transfer speed, in KB/s. rsync -avz --bwlimit=5000 big/ host:/dst/
--link-dest DIR Hard-link unchanged files from a previous snapshot — saves space in backups. rsync -av --link-dest=/bak/prev src/ /bak/today/
-c --checksum Compare files by checksum instead of modification time and size. rsync -avc src/ dst/

Nothing found. Try a shorter query.

Recipes

2

The trailing slash matters!

bash
rsync -av src/ dst/    # CONTENTS of src → into dst (dst/file)
rsync -av src  dst/    # the src directory ITSELF → into dst (dst/src/file)

A trailing slash on the source means "contents". No slash means "the directory itself". A frequent source of mistakes.

Commands

bash
rsync -avzP dir/ user@host:/backup/        # to a remote host over SSH, with progress
rsync -avz user@host:/data/ ./local/       # from a remote host
rsync -avzn --delete src/ dst/             # dry-run a mirror FIRST
rsync -avz --delete src/ dst/              # mirroring (deletes extra files in dst!)
rsync -avz --exclude='*.tmp' --exclude='cache/' src/ dst/
rsync -avz -e 'ssh -p 2222' src/ host:/    # non-default SSH port
rsync -avz --bwlimit=5000 big/ host:/      # don't saturate the link (5 MB/s)
# incremental snapshots (unchanged files are hard-linked to yesterday's):
rsync -av --delete --link-dest=/bak/2026-06-29 src/ /bak/2026-06-30/

Troubleshooting

5

--delete removed something needed

--delete makes dst an exact copy of src. Always run -n (dry-run) first and check the list. A safety net: --backup --backup-dir=.

Copied to the wrong place / a duplicated directory

The trailing slash on the source (see the slash section above).

It copies everything again every time

Comparison is by mtime and size; if time "drifts" (different filesystems or time zones), add -c (checksum comparison) or --modify-window=1.

Permissions/owners weren't preserved

You need -a (or -o/-g/-p individually); for foreign owners, root rights on the receiving side. On FAT/NTFS, owners are never preserved.

Slow with lots of small files

Normal (rsync checks every file). -z helps over a network; for a one-off archive in a single file, tar is faster.

Danger zone

--delete removes everything in the destination that isn't in the source, with no confirmation prompt and no trash bin: directories swapped by mistake will wipe production data in a single run. Always check such a command with -n (dry-run) and read the list of files to be deleted before removing the flag. A safety net for important data — --backup --backup-dir= instead of a direct delete.

Terms

4
directory mirror
A copy of a tree current state; without separate history it does not retain earlier versions.
dry run
A preview of proposed changes without applying them; it does not lock data against later changes. Also appears in: sysctl, unattended-upgrades: security updates with no one at the keyboard, diff and patch, git: change history, undo and a remote repository and 2 more
content checksum
A value computed from contents to detect differences; its role depends on the verification stage.
backup retention
Rules for retaining and removing previous backup states.

See also

  • tar — Creating and extracting tar/gz/xz archives; backing up directories.
  • ssh-client — Connecting over SSH, copying with scp/rsync, ~/.ssh/config, port forwarding.
  • files-basics — Navigation and files — ls, cd, cp, mv, rm, cat, less, head, tail, copy-paste in the terminal.
Loading…