linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Lessons
  • How it works
  • Simulator
  • Knowledge base
  • Interview prep
Index
Categories
All entries
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
home/linux/kb/Commands/cmd-rsync

kb/commands ── Commands ── beginner

rsync: incremental file synchronization

rsync copies only the changed blocks of files, locally or over SSH. `-avz` is the baseline combination (archive + verbose + compress). `--delete` mirrors. `--dry-run` is required before the first run.

view as markdownaka: rsync, rsync-command, rsync-sync

Why rsync

rsync copies a file tree between machines (or between directories on the same machine) with minimal traffic and time. When one byte changes in a gigabyte-sized file, rsync transfers a few kilobytes of metadata, not the whole file.

Comparison:

ToolWhen to use
cp -alocal copy, no network
scpone-off copy over SSH; does not split into blocks
rsyncrepeated synchronization; SSH or daemon
`tarssh tar`
rclonecloud storage (S3, GCS, Dropbox)

Basic syntax

rsync [OPTIONS] SRC [SRC]... DEST

Local:

bash
rsync -av /home/user/src/ /backup/src/

Over SSH (push):

bash
rsync -av /home/user/src/ user@host:/backup/src/

Over SSH (pull):

bash
rsync -av user@host:/var/log/ ./logs/

Trailing slash on the source: CRITICAL

/src/   - "contents of /src" -> copied into DEST
/src    - "the src directory itself" -> creates DEST/src/

90% of rsync bugs come from a missing or extra slash. Always add --dry-run before the first run.

-a: archive mode

This is shorthand for -rlptgoD:

  • -r recursive
  • -l preserve symlinks as symlinks
  • -p preserve permissions
  • -t preserve mtime (without this, rsync sends everything every time)
  • -g preserve group
  • -o preserve owner (root only)
  • -D special files (devices, sockets); rarely needed

Without -t, rsync detects changes by size. Files of equal size are skipped, which fails silently when a single byte changes inside a large file without altering the size. Always include -t.

Commonly used options

OptionEffect
-vverbose; -vv more verbose; -q quiet
-zon-the-fly compression (useful over a network, not locally)
-hhuman-readable sizes in KiB/MiB
-P--partial --progress (show progress and keep partial files)
-n--dry-run
--deleteremove from DEST anything absent in SRC (mirror)
--exclude=PATTERNskip matching files (can repeat)
--exclude-from=FILEread patterns from a file
--include=PATTERNoverride an exclude rule
--bwlimit=10Mcap bandwidth
-e sshspecify the SSH command (port, key)
--checksumcompare by content, not by mtime+size
--inplacewrite into existing file (for huge fixed-size files, databases)
--link-dest=DIRhard-link unchanged files from a previous backup (snapshot style)

--delete, mirroring

bash
rsync -av --delete /src/ /mirror/

After this run, /mirror/ is IDENTICAL to /src/. Files that exist in mirror but were removed from src are deleted. Always follow this sequence:

  1. Run --dry-run -v --delete first.
  2. Inspect the list of deleted files carefully.
  3. If it looks correct, remove -n.

This is especially dangerous in scripts that use variables:

bash
# DANGEROUS if SRC=""
rsync -av --delete "$SRC/" /backup/    # wipes all of /backup

Exclude

bash
rsync -av --exclude='node_modules' --exclude='.git' /src/ /backup/
rsync -av --exclude-from=.rsync-ignore /src/ /backup/

Patterns use shell-glob syntax, not regex. ** matches multiple directory levels. A trailing / matches directories only. Full rule syntax is in man rsync under "FILTER RULES".

SSH notes

bash
rsync -av -e "ssh -p 2222 -i ~/.ssh/key" /src/ user@host:/dst/

If SSH prompts for a password every time, use [[ssh|ssh-agent]] or a key without a passphrase. For cron jobs, a key is required.

Speed: -z spends CPU on compression. For already-compressed data (jpg, mp4, zip), -z hurts throughput. Turn it off.

Snapshot-style backups

The --link-dest trick:

bash
YESTERDAY=/backup/2026-05-01
TODAY=/backup/2026-05-02
rsync -av --delete --link-dest="$YESTERDAY" /data/ "$TODAY/"

Unchanged files become hard links to yesterday's copies, using zero extra disk space. Each 2026-MM-DD directory looks like a full copy. This is the pattern behind tools like rsnapshot and Time Machine.

When you delete old snapshots, the hard links resolve on their own and disk space is freed naturally.

When something goes wrong

  • Wrong ownership was copied: you ran rsync without root, so -o/-g had no effect. Use --no-perms --no-owner --no-group or run with sudo.
  • rsync resends everything on every run: mtime is not preserved (missing -t), or the destination filesystem does not support timestamps (FAT32, old SMB).
  • Permission denied (publickey): the SSH key was not passed via -e, or the remote user lacks access.
  • Speed stuck at 10 MB/s on a 1 Gbit link: -z is saturating the CPU. Remove it for already-compressed data.
  • Large files are interrupted when the connection drops: add -P (--partial) so the next run resumes from where it stopped.
  • --delete removed files you needed: --dry-run was not used. On important targets, add --backup --backup-dir=....
  • rsync error: some files/attrs were not transferred (8): usually an extended-attributes/ACL issue. Add -X -A or exclude the affected files.

Alternatives

  • rclone: rsync-style CLI for S3/GCS/Dropbox/etc.
  • bbcp, bbftp: high-throughput transfers over 10+ Gbit links
  • unison: bidirectional sync (rsync is one-way only)
  • borg/restic: dedup + encryption + snapshots; rsync loses ground to these for large-volume backups

§ команды

bash
rsync -avzP /src/ user@host:/dst/

Basic synchronization with progress display and compression

bash
rsync -avn --delete /src/ /backup/

Dry-run before mirroring: shows what will be added and deleted

bash
rsync -av --exclude='*.log' --exclude='node_modules' /app/ /backup/app/

Backup without noise: typical filter for application projects

bash
rsync -av --link-dest=/backup/yesterday /data/ /backup/today/

Snapshot backup with hard links: nearly free in disk space

bash
rsync -av --bwlimit=20M /src/ user@host:/dst/

Cap transfer rate at 20 MiB/s to avoid saturating the uplink during business hours

bash
rsync -av -e 'ssh -p 2222' /src/ user@host:/dst/

SSH on a non-standard port via -e

bash
rsync -avc /src/ /dst/

Compare by content (checksum): slower, but correct when mtime is unreliable

§ см. также

  • cmd-findfind: search files by predicates`find` walks a directory tree and applies predicates (name, type, time, size, permissions). Actions: `-print` (default), `-delete`, `-exec`, `| xargs`.
  • sshSSH: Secure ShellSSH is an encrypted channel to a remote host: shell, file copy, port-forwarding. Standard port 22, authentication by keys or password.
  • ftp-sftpFTP and SFTP: file transferFTP is an old protocol with control plus data connections (active/passive modes) and no encryption. SFTP is an SSH subsystem that shares nothing with FTP except the name. Today, use SFTP or [[cmd-rsync|rsync]].
  • nfsNFS: Network File SystemNFS is a network file system from Sun. v3 is stateless, v4.1+ is stateful with delegations and pNFS. /etc/exports on the server, mount -t nfs on the client. root_squash, sync/async, and the lock manager are the main options.
  • mount-and-fstabmount and /etc/fstab: attaching filesystems`mount` attaches a block device or filesystem to a mount point in the tree. `/etc/fstab` is the list of what to mount at boot.
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies