rsync is a fast and versatile incremental file-copying tool for local and remote file synchronization over SSH.
Trailing Slash Convention
[!IMPORTANT] Trailing Slash Matters:
src(without trailing slash): copies the directory itself (dest/src/files).src/(with trailing slash): copies only the contents insidesrc(dest/files).
# Sync contents of src directory into dest
rsync -avz ./src/ /var/backup/
Common Flag Combinations
# Standard archive sync with progress (-a: archive, -v: verbose, -z: compress, -h: human-readable)
rsync -avzh source/ [email protected]:/target/
# Mirror directory (deletes files in target that no longer exist in source)
rsync -avzh --delete source/ target/
# Dry run: preview changes without writing files
rsync -avzh --dry-run source/ target/
Exclusions & SSH Port
# Exclude git directories and node_modules
rsync -avz --exclude '.git' --exclude 'node_modules' ./src/ user@server:/app/
# Use non-standard SSH port (e.g. port 2222)
rsync -avz -e 'ssh -p 2222' ./dist/ user@remote:/www/
Bandwidth Limiting
# Limit transfer bandwidth to 5000 KB/s
rsync -avz --bwlimit=5000 large_dataset.tar remote:/backups/