rsync Commands

Efficient file synchronization and transfer tool with delta encoding, compression, and preservation options

Basic Synchronization

Basic local sync

rsync -av source/ destination/

Archive mode with verbose output. Preserves permissions, timestamps, symbolic links. Trailing slash on source matters.

Sync with progress display

rsync -av --progress source/ destination/

Show progress for each file being transferred. Useful for large transfers to monitor status.

Sync with human-readable output

rsync -avh --progress source/ destination/

Display file sizes in human-readable format (KB, MB, GB). Easier to understand transfer statistics.

Dry-run mode (preview without making changes)

rsync -avn --delete source/ destination/

Preview what would be transferred or deleted without actually making changes. Essential for testing.

Remote Transfers (SSH)

Sync to remote server

rsync -avz source/ user@remote:/path/to/destination/

Transfer files to remote server over SSH with compression. -z flag compresses during transfer.

Sync from remote server

rsync -avz user@remote:/path/to/source/ destination/

Download files from remote server to local machine. Same syntax reversed.

Sync with specific SSH port

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

Specify custom SSH port. Use -e flag to pass SSH options including port, identity file, etc.

Sync with SSH key authentication

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

Use specific SSH private key for authentication. Useful for automated backups with dedicated keys.

Archive Mode and Preservation

Archive mode breakdown

# -a is equivalent to -rlptgoD:
# -r: recursive
# -l: preserve symlinks
# -p: preserve permissions
# -t: preserve modification times
# -g: preserve group
# -o: preserve owner
# -D: preserve device and special files

rsync -rlptgoD source/ destination/

Archive mode is shorthand for common preservation options. Essential for accurate backups.

Preserve hard links

rsync -avH source/ destination/

Preserve hard links in addition to archive mode. Important for deduplication and system backups.

Preserve extended attributes and ACLs

rsync -avX --acls source/ destination/

Preserve extended attributes (-X) and access control lists (--acls). Critical for exact system replicas.

Update only newer files

rsync -avu source/ destination/

Skip files that are newer in destination. -u prevents overwriting newer files with older versions.

Deletion Options

Delete files not in source

rsync -av --delete source/ destination/

Make destination identical to source by deleting extra files. Essential for true synchronization.

Delete before transfer

rsync -av --delete-before source/ destination/

Delete files in destination before transferring new files. Frees space before copying.

Delete during transfer

rsync -av --delete-during source/ destination/

Delete files incrementally during transfer. More efficient for large syncs.

Delete excluded files

rsync -av --delete --exclude='*.tmp' source/ destination/

Delete files matching exclusion pattern in destination. Useful for cleaning temporary files.

Exclude and Include Patterns

Exclude specific files or patterns

rsync -av --exclude='*.log' --exclude='tmp/' source/ destination/

Skip files matching patterns. Use multiple --exclude flags for different patterns.

Exclude from file

rsync -av --exclude-from=exclude-list.txt source/ destination/

Read exclusion patterns from file. One pattern per line. Easier to manage complex exclusions.

Include specific files only

rsync -av --include='*.txt' --exclude='*' source/ destination/

Only sync files matching include pattern. Order matters: includes before excludes.

Exclude common temporary files

rsync -av --exclude='*.swp' --exclude='*.tmp' --exclude='.DS_Store' \
  --exclude='node_modules/' source/ destination/

Common exclusions for development environments and system files.

Bandwidth and Performance

Limit bandwidth usage

rsync -av --bwlimit=1000 source/ user@remote:/destination/

Limit bandwidth to 1000 KB/s. Prevents rsync from saturating network connection.

Enable compression

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

Compress data during transfer. Reduces bandwidth but increases CPU usage. Best for slow networks.

Skip compression for specific file types

rsync -avz --skip-compress=gz/zip/jpg/png source/ user@remote:/destination/

Don't compress already-compressed formats. Saves CPU cycles without sacrificing bandwidth.

Use partial transfers

rsync -avz --partial --progress source/ user@remote:/destination/

Keep partially transferred files. Resume interrupted transfers instead of restarting from scratch.

Combined partial and progress (shorthand)

rsync -avz -P source/ user@remote:/destination/

-P is shorthand for --partial --progress. Commonly used for reliable remote transfers.

Backup and Snapshot Options

Create backup of overwritten files

rsync -av --backup --backup-dir=../backup source/ destination/

Save overwritten or deleted files to backup directory before sync. Safety net for destructive syncs.

Timestamped backup directory

rsync -av --backup --backup-dir=backup-$(date +%Y%m%d) source/ destination/

Create dated backup directories. Useful for keeping multiple backup versions over time.

Incremental backup with hard links

rsync -avH --link-dest=../backup-previous source/ backup-current/

Create space-efficient incremental backups. Unchanged files are hard-linked to previous backup.

Suffix for backup files

rsync -av --backup --suffix=.bak source/ destination/

Add custom suffix to backup files instead of default tilde. Easier to identify backup files.

Statistics and Logging

Show transfer statistics

rsync -av --stats source/ destination/

Display detailed statistics after transfer: number of files, total size, speedup achieved.

Human-readable statistics

rsync -avh --stats source/ destination/

Show statistics with human-readable units (KB, MB, GB). Easier to interpret large numbers.

Log to file

rsync -av --log-file=rsync.log source/ destination/

Write detailed log to file. Essential for automated backups and troubleshooting.

Itemized changes

rsync -avi source/ destination/

Show detailed item changes with codes (file type, permissions, ownership changes). Very verbose.

Quiet mode

rsync -aq source/ destination/

Suppress non-error messages. Useful for cron jobs where you only want to see problems.

Advanced Options

Checksum-based comparison

rsync -avc source/ destination/

Compare files by checksum instead of size and timestamp. Slower but more accurate detection.

Sparse file handling

rsync -avS source/ destination/

Handle sparse files efficiently. Important for VM disk images and database files.

Preserve device files

rsync -avD source/ destination/

Preserve device and special files. Required for full system backups.

Copy unsafe symlinks

rsync -av --copy-unsafe-links source/ destination/

Copy symlinks that point outside source tree as files. Prevents broken links in destination.

Ignore existing files

rsync -av --ignore-existing source/ destination/

Skip files that already exist in destination. Only add new files, never update existing ones.

Size-only comparison

rsync -av --size-only source/ destination/

Compare files by size only, ignore timestamps. Faster but less accurate than default.

Practical Examples

Complete server backup

rsync -azvhP --delete --exclude='/dev' --exclude='/proc' --exclude='/sys' \
  --exclude='/tmp' --exclude='/run' --exclude='/mnt' --exclude='/media' \
  user@server:/ /backup/server/

Full system backup excluding virtual filesystems and temporary directories.

Website deployment

rsync -avz --delete --exclude='.git' --exclude='node_modules' \
  --exclude='.env' ./dist/ user@webserver:/var/www/html/

Deploy website files excluding development and sensitive files.

Incremental daily backup

#!/bin/bash
DATE=$(date +%Y-%m-%d)
BACKUP_DIR=/backups
LATEST=$BACKUP_DIR/latest

rsync -avH --delete --link-dest=$LATEST \
  /data/ $BACKUP_DIR/$DATE/

rm -f $LATEST
ln -s $DATE $LATEST

Space-efficient incremental backups using hard links. Only changed files consume additional space.

Certificate and key backup

rsync -avz --backup --backup-dir=/backup/certs-$(date +%Y%m%d) \
  /etc/ssl/ user@backup-server:/backup/ssl/

Backup SSL/TLS certificates and keys with timestamped backup directory.

Mirror with bandwidth limit (slow connection)

rsync -avzP --bwlimit=500 --delete \
  source/ user@remote:/destination/

Sync large directories over slow connection without saturating bandwidth. Resume on interruption.

Important Notes

Trailing Slash Matters

source/ copies contents of source directory. source (no slash) copies the directory itself. This is critical for correct behavior.

Test with Dry-Run

Always test destructive operations (--delete) with -n flag first. Preview changes before applying them.

SSH Key Authentication

For automated backups, set up SSH keys without passphrases or use ssh-agent. Password prompts break automation.

Compression Trade-offs

Use -z for slow networks, skip it for fast LANs or when transferring compressed files. Compression uses CPU.

Permissions and Ownership

-a preserves ownership, but target user must have permissions. Use sudo on remote side if needed for system files.

Delta Transfer Algorithm

rsync only transfers changed portions of files, not entire files. This makes it extremely efficient for large file updates.