Linux Command Reference ยท Archives and Compression
Linux rsync command
Reference for rsync on Linux: sync directories locally or over SSH, the trailing-slash rule, --delete, --dry-run, exclude patterns, and safe backup options.
rsync makes a destination look like a source while transferring as little data as possible. It compares files by size and modification time (or by checksum), then sends only what changed, using a delta algorithm for files that differ partly. It works locally, over SSH, or against an rsync daemon, and it is the standard tool for backups, deployments and mirroring.
Most of its power comes from one habit: preview with --dry-run first, then run for real.
rsync -a SOURCE/ DEST/ rsync -avh --delete --dry-run SOURCE/ user@host:/backup/
Try it
rsync is not installed in our sandbox. This example shows the most important rule, the trailing slash, with cp -a, which follows the same convention for the source name src/. versus src. The rsync lines are read-only.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Read-only: a dry run shows what would change without touching anything.
$ rsync -avh --dry-run --delete src/ backup/ sending incremental file list deleting old.txt a.txt sub/b.txt sent 160 bytes received 28 bytes 376.00 bytes/sec total size is 8 speedup is 0.04 (DRY RUN)
Syntax
rsync [OPTION]... SRC... DEST rsync [OPTION]... SRC... [USER@]HOST:DEST rsync [OPTION]... [USER@]HOST:SRC... DEST
| Option | Description |
|---|---|
-a |
Archive mode: recursive, copies symlinks, permissions, times, group, owner and devices (-rlptgoD). |
-v, -h |
Verbose output; human-readable sizes. |
-n, --dry-run |
Show what would happen; change nothing. |
--delete |
Remove files from DEST that no longer exist in SRC. |
-z |
Compress data in transit. |
-P |
Show progress and keep partial files (--partial --progress). |
--exclude=PATTERN |
Skip matching paths. Repeat as needed. |
--include=PATTERN |
Re-include a path; order matters with --exclude. |
-e CMD |
Remote shell, for example -e "ssh -p 2222". |
-c |
Compare by checksum instead of size and time (slower, more exact). |
--backup --backup-dir=DIR |
Move replaced or deleted files into DIR. |
--link-dest=DIR |
Hard-link unchanged files against an earlier snapshot. |
--info=progress2 |
One overall progress line for the whole transfer. |
Returns: 0 on success; 23 for partial transfer due to errors, 24 when source files vanished during the run, 12 for protocol or data stream errors, 30 for a timeout.
Usage notes
- The trailing slash on the source decides everything.
rsync -a src/ dest/copies the contents intodest;rsync -a src dest/createsdest/src. A trailing slash on the destination does not matter. --deleteis destructive. It removes files at the destination. Always run with-nfirst, and double-check that SRC is not empty or unmounted, or an automated job can wipe a backup.-ais not everything. Add-Hfor hard links,-Afor ACLs,-Xfor extended attributes. Preserving owners needs root on the receiving side.- Excludes are relative to the transfer root.
--exclude='/build'anchors at the top,--exclude='*.log'matches anywhere. Quote patterns so the shell leaves them alone. - Remote paths with spaces need quoting twice in old versions; modern rsync handles
--protect-argsor-s. - Interrupted transfers resume with
--partial(included in-P); re-running the same command skips what is already done. - Exit code 23: read the errors above it; permission-denied files are skipped, the rest is copied.
- Snapshots:
--link-destcreates dated, space-efficient backup trees, where unchanged files are hard links to the previous run.
Related
cp,tar,scp and sftp,ssh,cpio- Lessons: SSH and Remote Work, Backup Project