Skip to content

rsync Command Cheat Sheet

rsync is a fast, versatile file copying and synchronization tool. It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon. It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source and destination files.


Synopsis

rsync [OPTIONS] SOURCE DESTINATION
rsync [OPTIONS] SOURCE... DESTINATION
rsync [OPTIONS] SOURCE [USER@]HOST:DESTINATION
rsync [OPTIONS] [USER@]HOST:SOURCE DESTINATION

Description

rsync is a replacement for rcp that has many more features. It uses the "rsync algorithm" which provides a very fast method for bringing remote files into sync. It does this by sending just the differences in the files across the link, without requiring that both sets of files are present at one of the ends of the link beforehand.


Basic Usage

Local File Copy

rsync -av source/ destination/

Local File Sync

# Sync directory to another location
rsync -av /source/directory/ /backup/directory/

Important: Trailing slash matters! - source/ - copies contents OF source - source - copies source directory ITSELF


Common Options

-a, --archive

Archive mode (preserve almost everything).

Equivalent to -rlptgoD: - -r: Recursive - -l: Copy symlinks as symlinks - -p: Preserve permissions - -t: Preserve modification times - -g: Preserve group - -o: Preserve owner (super-user only) - -D: Preserve device and special files

rsync -a source/ dest/

-v, --verbose

Increase verbosity (show files being transferred).

rsync -av source/ dest/

-z, --compress

Compress file data during transfer.

rsync -avz source/ user@remote:/dest/

-h, --human-readable

Output numbers in human-readable format.

rsync -avh source/ dest/

-P

Combination of --partial and --progress.

rsync -avP source/ dest/

Shows progress and keeps partially transferred files.

--progress

Show progress during transfer.

rsync -av --progress source/ dest/

-n, --dry-run

Perform trial run with no changes made.

rsync -avn source/ dest/

Perfect for testing before actual sync!


Remote Synchronization

Sync to Remote Server (Push)

rsync -avz source/ user@remote.server.com:/path/to/dest/

Sync from Remote Server (Pull)

rsync -avz user@remote.server.com:/path/to/source/ /local/dest/

Using Custom SSH Port

rsync -avz -e "ssh -p 2222" source/ user@remote:/dest/

Using SSH Key

rsync -avz -e "ssh -i ~/.ssh/id_rsa" source/ user@remote:/dest/

Using Different SSH Options

rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" source/ user@remote:/dest/

Exclude and Include Patterns

Exclude Files/Directories

# Exclude single pattern
rsync -av --exclude='*.log' source/ dest/

# Exclude multiple patterns
rsync -av --exclude='*.log' --exclude='*.tmp' --exclude='cache/' source/ dest/

Exclude from File

Create .rsyncignore:

*.log
*.tmp
cache/
node_modules/
.git/

Use it:

rsync -av --exclude-from='.rsyncignore' source/ dest/

Include Specific Files

# Include only .txt files
rsync -av --include='*.txt' --exclude='*' source/ dest/

# Include .txt and .md, exclude rest
rsync -av --include='*.txt' --include='*.md' --exclude='*' source/ dest/

Complex Include/Exclude

# Include .jpg from photos/, exclude everything else
rsync -av \
  --include='photos/' \
  --include='photos/*.jpg' \
  --exclude='*' \
  source/ dest/

Delete Options

--delete

Delete files in dest that don't exist in source.

rsync -av --delete source/ dest/

Makes dest a mirror of source.

--delete-before

Delete before transfer (default).

rsync -av --delete-before source/ dest/

--delete-during

Delete during transfer (faster).

rsync -av --delete-during source/ dest/

--delete-after

Delete after transfer (safer).

rsync -av --delete-after source/ dest/

--delete-excluded

Delete excluded files from destination.

rsync -av --delete-excluded --exclude='*.log' source/ dest/

Bandwidth and Performance

Limit Bandwidth

# Limit to 1 MB/s (1000 KB/s)
rsync -av --bwlimit=1000 source/ user@remote:/dest/

# Limit to 100 KB/s
rsync -av --bwlimit=100 source/ user@remote:/dest/

Partial Transfer

# Keep partially transferred files
rsync -av --partial source/ dest/

Compress During Transfer

rsync -avz source/ user@remote:/dest/

Checksum-based Comparison

# Use checksum instead of mod-time & size
rsync -avc source/ dest/

Slower but more accurate.

Sparse Files

rsync -avS source/ dest/

Handle sparse files efficiently.


Advanced Options

Update Only (Skip Newer Files)

rsync -avu source/ dest/

Ignore Existing Files

rsync -av --ignore-existing source/ dest/

Only copy new files, never overwrite.

Size Only Comparison

rsync -av --size-only source/ dest/

Skip files with same size (ignore timestamp).

Skip Newer Files in Destination

rsync -av --update source/ dest/
rsync -avH source/ dest/

Preserve Extended Attributes

rsync -avX source/ dest/

Preserve ACLs

rsync -avA source/ dest/

Preserve Everything

rsync -avHAX source/ dest/

Backup and Synchronization

# First backup
rsync -av source/ /backup/full/

# Incremental backups (hardlinks to unchanged files)
rsync -av --link-dest=/backup/full/ source/ /backup/increment1/
rsync -av --link-dest=/backup/increment1/ source/ /backup/increment2/

Backup with Timestamp

rsync -av source/ /backup/backup-$(date +%Y%m%d-%H%M%S)/

Mirror with Deletions

rsync -av --delete source/ dest/

Backup Deleted Files

rsync -av --backup --backup-dir=/backup/deleted-$(date +%F) source/ dest/

Practical Examples

Website Deployment

rsync -avz --delete \
  --exclude='.git' \
  --exclude='node_modules' \
  --exclude='.env' \
  /local/website/ user@server:/var/www/html/

Database Backup

rsync -avz user@db-server:/var/lib/mysql/ /backup/mysql/

Home Directory Backup

rsync -avh --progress \
  --exclude='.cache' \
  --exclude='Downloads' \
  --exclude='.local/share/Trash' \
  ~ /backup/home/

Server to Server Transfer

# Via SSH tunnel
rsync -avz -e ssh server1:/data/ server2:/data/

Sync Only Changed Files

rsync -avzu --progress source/ dest/

Copy with Progress and Stats

rsync -avh --progress --stats source/ dest/

Dry Run Before Actual Sync

# Test first
rsync -avn --delete source/ dest/

# If OK, run for real
rsync -av --delete source/ dest/

Resume Interrupted Transfer

rsync -avP source/ user@remote:/dest/

-P = --partial --progress

Sync Entire System (Excluding Some Directories)

sudo rsync -aAXv --delete \
  --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} \
  / /backup/system/

Logging and Output

Log to File

rsync -av --log-file=/var/log/rsync.log source/ dest/

Verbose Output Levels

rsync -v source/ dest/      # Basic
rsync -vv source/ dest/     # More details
rsync -vvv source/ dest/    # Maximum verbosity

Show Item Changes

rsync -avi source/ dest/

Output format:

>f+++++++++ file.txt    # New file
.f..t...... file.txt    # Timestamp changed
>f.st...... file.txt    # Size and timestamp changed
cd+++++++++ dir/        # New directory

Statistics

rsync -av --stats source/ dest/

Shows transfer statistics at end.


Troubleshooting

Ignore Permissions Errors

rsync -rltvz --no-perms --no-owner --no-group source/ dest/

Force Deletion of Non-Empty Directories

rsync -av --delete --force source/ dest/
rsync -avl source/ dest/

Continue on Errors

rsync -av --ignore-errors source/ dest/

Verify After Transfer

# Transfer
rsync -av source/ dest/

# Verify
rsync -avc source/ dest/

No output = perfect sync.


rsync Over SSH Tunnel

With Custom Port

rsync -avz -e "ssh -p 2222" source/ user@host:/dest/

With Compression

rsync -avz -e "ssh -C" source/ user@host:/dest/

With Multiple SSH Options

rsync -avz -e "ssh -p 2222 -i ~/.ssh/key -o StrictHostKeyChecking=no" source/ user@host:/dest/

Comparison

rsync vs scp

Feature rsync scp
Delta transfer
Resume capability Limited
Preserve attributes
Bandwidth limiting
Exclude patterns
Synchronization
Speed (incremental) Fast Slow

rsync vs cp

Feature rsync cp
Remote copy
Delta transfer
Resume capability
Progress display
Bandwidth limiting
Exclude patterns

Common Options Summary

Option Description
-a Archive mode (recursive, preserve almost everything)
-v Verbose
-z Compress during transfer
-h Human-readable sizes
-P Show progress, keep partial files
-n Dry run
-u Update only (skip newer files)
-r Recursive
-l Copy symlinks as symlinks
-p Preserve permissions
-t Preserve modification times
-g Preserve group
-o Preserve owner
-D Preserve device files
-H Preserve hard links
-A Preserve ACLs
-X Preserve extended attributes
-S Handle sparse files efficiently
--delete Delete extraneous files from dest
--exclude Exclude pattern
--include Include pattern
--bwlimit Limit bandwidth (KB/s)
--progress Show progress
--partial Keep partially transferred files
--stats Show transfer statistics
-e Specify remote shell

Exit Status

Code Meaning
0 Success
1 Syntax or usage error
2 Protocol incompatibility
3 Errors selecting input/output files, dirs
4 Unsupported action
5 Error starting client-server protocol
6 Daemon unable to append to log-file
10 Error in socket I/O
11 Error in file I/O
12 Error in rsync protocol data stream
13 Errors with program diagnostics
14 Error in IPC code
20 Received SIGUSR1 or SIGINT
21 Some error returned by waitpid()
22 Error allocating core memory buffers
23 Partial transfer due to error
24 Partial transfer due to vanished source files
25 Maximum --max-delete limit stopped deletions
30 Timeout in data send/receive
35 Timeout waiting for daemon connection

Tips and Best Practices

  1. Always test with -n first - Dry run prevents mistakes
  2. Use -P for large transfers - Resume + progress monitoring
  3. Trailing slash matters - dir/ vs dir behaves differently
  4. Use --delete carefully - Can cause data loss if misconfigured
  5. Add --dry-run for safety - Test before running destructive operations
  6. Use -z for remote - Compression saves bandwidth
  7. Combine options efficiently - -avzP is common for remote sync
  8. Log important syncs - Use --log-file for auditing
  9. Exclude version control - Add --exclude='.git' for deployments
  10. Verify critical syncs - Run with -c to verify checksums