Linux CLI 40 ๐ง rsync command
rsync -av /home/kr/cli /home/kr/clibkp
rsync -avz user@192.168.1.100:/home/kr/cli /home/kr/clibkp
rsync -av --delete /home/kr/cli user@192.168.1.100:/home/kr/cli
rsync -avzP /home/kr/cli user@192.168.1.100:/home/kr/cli
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
ssh-copy-id user@remote_host
rsync -avz --delete /source/dir/ user@remote_host:/destination/dir/
rsync is the workhorse of file synchronization on Linux. It can copy files locally, transfer them across the network over SSH, and โ most importantly โ synchronize two locations so they match. It only transfers what has changed, which makes it dramatically faster than cp or scp for repeated operations.
Key point: rsync is incremental. The first run copies everything; later runs copy only the differences. Add --delete and it becomes a true mirror โ the destination is made to look exactly like the source.
a – rsync command
rsync is a powerful command used to transfer files over a network. It can copy files between local and remote systems and synchronize directories. It supports various options to control the transfer process.
Common options:
| Option | Purpose |
|---|---|
-a | Archive mode โ preserves permissions, timestamps, symlinks, ownership |
-v | Verbose mode |
-z | Compress data during transfer |
-P | Show progress and support pausing/resuming |
-e | Specify an alternative remote shell (-e ssh) |
-r | Recursive (for directories) |
-n | Dry run โ show what would happen without doing it |
--delete | Delete files in destination that don’t exist in source |
--exclude | Skip matching files |
--progress | Show per-file progress |
-h | Human-readable numbers |
Syntax:
rsync [options] [src] [user@]host:target
rsync [options] [user@]host:src [target]
The colon is what makes a path remote. host:/path is remote; /path is local.
Examples:
# Local copy
$ rsync -av /home/kr/cli /home/kr/clibkp
sending incremental file list
cli/
cli/file1.txt
cli/file2.txt
cli/subdir/
cli/subdir/file3.txt
sent 1,234 bytes received 567 bytes 3,602.00 bytes/sec
total size is 10,240 speedup is 5.68
# Remote โ local
$ rsync -avz user@192.168.1.100:/home/kr/cli /home/kr/clibkp
receiving incremental file list
cli/
cli/file1.txt
...
# Local โ remote with compression and progress
$ rsync -avzP /home/kr/cli user@192.168.1.100:/home/kr/cli
sending incremental file list
cli/
cli/file1.txt
1,024 100% 1.02MB/s 0:00:00 (xfr#1, to-chk=3/5)
...
# Sync with deletion of removed files
$ rsync -av --delete /home/kr/cli user@192.168.1.100:/home/kr/cli
# Dry run first (very important with --delete!)
$ rsync -avn --delete /home/kr/cli user@192.168.1.100:/home/kr/cli
# Exclude patterns
$ rsync -av --exclude='*.log' --exclude='.git' /home/kr/cli /backup/
# Specify SSH explicitly
$ rsync -avz -e ssh /home/kr/cli user@192.168.1.100:/home/kr/cli
# Use a custom SSH port
$ rsync -avz -e 'ssh -p 2222' /home/kr/cli user@192.168.1.100:/home/kr/cli
# Human-readable output
$ rsync -avh /home/kr/cli /backup/
# Show per-file progress
$ rsync -av --progress /home/kr/cli /backup/
How the trailing slash changes everything:
# With trailing slash on source โ copies CONTENTS into target
$ rsync -av /home/kr/cli/ /home/kr/clibkp/
# Result: /home/kr/clibkp/file1.txt, /home/kr/clibkp/file2.txt
# Without trailing slash โ copies the DIRECTORY itself into target
$ rsync -av /home/kr/cli /home/kr/clibkp/
# Result: /home/kr/clibkp/cli/file1.txt, /home/kr/clibkp/cli/file2.txt
This is the single most common source of confusion with rsync. A trailing slash on the source means “copy the contents.” No trailing slash means “copy the directory.”
How rsync’s delta algorithm works:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ First rsync run โ
โ โ
โ Source: file1, file2, file3 โ
โ Destination: (empty) โ
โ โ
โ โ copies all three files โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Second rsync run โ
โ โ
โ Source: file1, file2', file3 โ
โ Destination: file1, file2, file3 โ
โ โ
โ โ file2 changed, only file2' is transferred โ
โ โ files 1 and 3 are skipped (same size/mtime)โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
rsync compares size and modification time by default. If they match, it assumes the file is unchanged and skips it. This is what makes it fast.
b – rsync examples
Here are the most common real-world uses of rsync, from simple local copies to full remote synchronization.
1. Local copy:
$ rsync -av /home/kr/cli /home/kr/clibkp
Copies the cli directory to clibkp. Preserves permissions, timestamps, and symlinks. On later runs, only changed files are copied.
2. Copy from remote to local:
$ rsync -avz user@192.168.1.100:/home/kr/cli /home/kr/clibkp
Copies everything from the remote machine to the local one. The -z compresses data in transit โ good for slow links.
3. Synchronize directories and delete extras:
$ rsync -av --delete /home/kr/cli user@192.168.1.100:/home/kr/cli
Synchronizes the two directories and deletes files in the destination that don’t exist in the source. This makes the destination an exact mirror.
โ ๏ธ Warning:
--deleteis destructive. Always run with-n(dry run) first to see what would be deleted.
4. Pause and resume:
$ rsync -avzP /home/kr/cli user@192.168.1.100:/home/kr/cli
Press Ctrl+C to pause; run the same command to resume. The -P flag enables --partial and --progress, so partially transferred files are kept and resumed instead of restarted.
5. SSH key-based authentication:
If you frequently use rsync between two machines, set up SSH keys so you don’t type a password every time.
# Generate a key pair
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/kr/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/kr/.ssh/id_rsa
Your public key has been saved in /home/kr/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:abc123def456... your_email@example.com
# Copy the public key to the remote machine
$ ssh-copy-id user@remote_host
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
...
Number of key(s) added: 1
# Now rsync runs without asking for a password
$ rsync -avzP /home/kr/cli user@192.168.1.100:/home/kr/cli
6. Full sync with key auth:
$ rsync -avz --delete /source/dir/ user@remote_host:/destination/dir/
The classic backup/mirror command. Compressed, incremental, deletes extras on the destination, and runs without a password thanks to the SSH key.
More examples:
# Dry run (safe preview)
$ rsync -avn --delete /source/ user@host:/dest/
# Exclude patterns
$ rsync -av --exclude='*.tmp' --exclude='node_modules' /project/ /backup/
# Exclude via file
$ rsync -av --exclude-from=.rsyncignore /project/ /backup/
# Bandwidth limit (KB/s)
$ rsync -avz --bwlimit=1000 /source/ user@host:/dest/
# Preserve hard links
$ rsync -avH /source/ /dest/
# Preserve ACLs and extended attributes
$ rsync -avAX /source/ /dest/
# Delete excluded files on destination too
$ rsync -av --delete --delete-excluded --exclude='*.log' /source/ /dest/
# Sync into an existing directory without renaming
$ rsync -av /source/. /dest/
# Show what would be transferred without -v noise
$ rsync -an --stats /source/ /dest/
# Back up with a timestamped directory
$ rsync -av /source/ /backup/$(date +%F)/
# Use rsync as a faster cp
$ rsync -av /src/ /dst/
Complete Example Session
# ============================================
# PART 1: LOCAL COPY
# ============================================
$ rsync -av /home/kr/cli /home/kr/clibkp
sending incremental file list
created directory /home/kr/clibkp
cli/
cli/file1.txt
cli/file2.txt
cli/subdir/
cli/subdir/file3.txt
sent 1,234 bytes received 567 bytes 3,602.00 bytes/sec
total size is 10,240 speedup is 5.68
# Run again โ only changed files transfer
$ rsync -av /home/kr/cli /home/kr/clibkp
sending incremental file list
sent 123 bytes received 17 bytes 280.00 bytes/sec
total size is 10,240 speedup is 73.14
# โ
Nothing to copy โ fast!
# ============================================
# PART 2: REMOTE COPY (REMOTE โ LOCAL)
# ============================================
$ rsync -avz user@192.168.1.100:/home/kr/cli /home/kr/clibkp
receiving incremental file list
cli/
cli/file1.txt
cli/file2.txt
cli/subdir/file3.txt
sent 123 bytes received 1,234 bytes 904.67 bytes/sec
total size is 10,240 speedup is 7.54
# ============================================
# PART 3: SYNC WITH --DELETE
# ============================================
# Dry run first
$ rsync -avn --delete /home/kr/cli user@192.168.1.100:/home/kr/cli
sending incremental file list
deleting oldfile.txt
cli/
cli/file4.txt
# Then for real
$ rsync -av --delete /home/kr/cli user@192.168.1.100:/home/kr/cli
sending incremental file list
deleting oldfile.txt
cli/file4.txt
sent 1,234 bytes received 567 bytes 3,602.00 bytes/sec
total size is 12,288 speedup is 6.82
# ============================================
# PART 4: PAUSE AND RESUME
# ============================================
$ rsync -avzP /home/kr/cli user@192.168.1.100:/home/kr/cli
sending incremental file list
cli/
cli/bigfile.iso
524,288,000 50% 10.20MB/s 0:00:50 (xfr#1, to-chk=2/5)
^C
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20)
# Run the same command to resume
$ rsync -avzP /home/kr/cli user@192.168.1.100:/home/kr/cli
sending incremental file list
cli/bigfile.iso
524,288,000 50% 10.20MB/s 0:00:50 (xfr#1, to-chk=2/5)
# โ
Resumes from where it stopped
# ============================================
# PART 5: SSH KEY AUTHENTICATION
# ============================================
# Generate a key pair
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/kr/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/kr/.ssh/id_rsa
Your public key has been saved in /home/kr/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:abc123def456... your_email@example.com
# Copy the public key to the remote
$ ssh-copy-id user@remote_host
Number of key(s) added: 1
# Test passwordless login
$ ssh user@remote_host
# โ
no password prompt
# Now rsync without password
$ rsync -avzP /home/kr/cli user@192.168.1.100:/home/kr/cli
# โ
no password prompt
# ============================================
# PART 6: FULL SYNC COMMAND
# ============================================
$ rsync -avz --delete /source/dir/ user@remote_host:/destination/dir/
sending incremental file list
deleting oldfile.txt
file1.txt
file2.txt
...
sent 1,234 bytes received 567 bytes 3,602.00 bytes/sec
total size is 10,240 speedup is 5.68
# ============================================
# PART 7: EXCLUDE PATTERNS
# ============================================
$ rsync -av --exclude='*.log' --exclude='node_modules' --exclude='.git' \
/home/kr/project/ /backup/project/
sending incremental file list
project/
project/src/
project/src/main.c
project/README.md
# โ
.log, node_modules, .git skipped
# ============================================
# PART 8: BANDWIDTH LIMIT
# ============================================
$ rsync -avz --bwlimit=1000 /home/kr/bigdata/ user@host:/backup/
# Limited to ~1 MB/s
# ============================================
# PART 9: STATS
# ============================================
$ rsync -an --stats /source/ /dest/
Number of files: 123
Number of created files: 0
Number of deleted files: 0
Number of regular files transferred: 0
Total file size: 10,240 bytes
Total transferred file size: 0 bytes
Literal data: 0 bytes
Matched data: 0 bytes
File list size: 0
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 123
Total bytes received: 17
# โ
Nothing to do
Quick Reference
rsync โ Core Options
| Option | Purpose |
|---|---|
-a | Archive mode (perms, times, links) |
-v | Verbose |
-z | Compress in transit |
-P | Progress + partial (resume) |
-r | Recursive |
-n | Dry run |
-h | Human-readable |
--delete | Delete extras in destination |
--exclude=PAT | Skip matching files |
--exclude-from=FILE | Read exclude patterns from file |
--progress | Show per-file progress |
--stats | Show summary statistics |
--bwlimit=KBPS | Limit bandwidth |
-e ssh | Use SSH explicitly |
-H | Preserve hard links |
-A | Preserve ACLs |
-X | Preserve extended attributes |
rsync โ Common Recipes
| Command | Purpose |
|---|---|
rsync -av SRC DST | Local copy |
rsync -avz user@host:SRC DST | Remote โ local |
rsync -avz SRC user@host:DST | Local โ remote |
rsync -av --delete SRC DST | Mirror (delete extras) |
rsync -avn --delete SRC DST | Dry-run mirror |
rsync -avzP SRC user@host:DST | Resumable transfer |
rsync -av -e 'ssh -p 2222' SRC DST | Custom SSH port |
rsync -av --exclude='*.log' SRC DST | Exclude patterns |
rsync -av --bwlimit=1000 SRC DST | Limit speed |
rsync -av SRC/ DST/ | Copy contents (trailing slash) |
rsync -av SRC DST/ | Copy directory itself |
SSH Keys
| Command | Purpose |
|---|---|
ssh-keygen -t rsa -b 4096 -C "email" | Generate RSA-4096 key |
ssh-keygen -t ed25519 -C "email" | Generate Ed25519 key (modern) |
ssh-copy-id user@host | Copy public key to remote |
ssh user@host | Test passwordless login |
Best Practices
โ Do This:
# Always dry-run with --delete
rsync -avn --delete SRC DST # โ
# Use -a to preserve metadata
rsync -av SRC DST # โ
# Use -z over slow links
rsync -avz SRC user@host:DST # โ
# Use -P for large transfers (resumable)
rsync -avzP SRC user@host:DST # โ
# Set up SSH keys for frequent syncs
ssh-keygen && ssh-copy-id user@host # โ
# Trailing slash = contents
rsync -av SRC/ DST/ # โ
copies contents
rsync -av SRC DST/ # โ
copies directory
# Exclude junk
rsync -av --exclude='.git' SRC DST # โ
# Use --bwlimit to avoid saturating links
rsync -avz --bwlimit=1000 SRC DST # โ
# Verify with --stats on dry run
rsync -an --stats SRC DST # โ
โ Don’t Do This:
# Don't use --delete without a dry run first
rsync -av --delete SRC DST # โ dangerous
# Don't confuse trailing slashes
rsync -av SRC DST # โ creates DST/SRC
rsync -av SRC/ DST/ # โ
contents only
# Don't use rsync for one-time copies (use cp)
rsync -av SRC DST # โ ๏ธ overkill for a one-off
# Don't forget -z on slow networks
rsync -av SRC user@host:DST # โ slower
# Don't sync without key auth for automation
rsync -av SRC user@host:DST # โ password prompts
# Don't sync a live database without quiescing
rsync -av /var/lib/mysql/ /backup/ # โ inconsistent
# Don't run --delete in the wrong direction
rsync -av --delete EMPTY/ FULL/ # โ wipes FULL
Common Pitfalls
| Pitfall | Problem | Solution |
|---|---|---|
| Trailing slash confusion | Files land in wrong place | Remember: slash = contents |
--delete deletes too much | Data loss | Dry run first with -n |
No -a | Permissions/times not preserved | Use -av |
| Slow over network | No compression | Add -z |
| Password prompts | No key auth | Set up SSH keys |
| Partial transfer lost | No -P | Use -avzP |
| Files skipped unexpectedly | Size+mtime match | Use -c (checksum) |
Wrong direction with --delete | Wipes source of truth | Double-check SRC/DST |
| Symlinks followed | Unexpected copies | Use -a (preserves links) |
.git copied | Huge unnecessary transfer | Exclude it |
Real-World Examples
1. Back Up Your Home Directory
$ rsync -av --delete /home/kr/ /backup/home/
sending incremental file list
...
sent 1,234,567 bytes received 8,901 bytes 2,486,936.00 bytes/sec
total size is 42,000,000,000 speedup is 33,800.00
2. Mirror a Website to a Remote Server
$ rsync -avz --delete /var/www/html/ user@192.168.1.100:/var/www/html/
3. Sync a Project to a Server for Testing
$ rsync -avzP --exclude='.git' --exclude='node_modules' \
/home/kr/project/ user@test-server:/srv/project/
4. Download a Large Directory from a Server
$ rsync -avzP user@192.168.1.100:/data/ /local/data/
5. Timestamped Backups
$ rsync -av /home/kr/documents/ /backup/documents-$(date +%F)/
6. Resume an Interrupted Transfer
# Ctrl+C to stop
$ rsync -avzP /data/ user@host:/backup/
^C
# Run the same command to resume
$ rsync -avzP /data/ user@host:/backup/
7. Bandwidth-Limited Sync Over a Slow Link
$ rsync -avz --bwlimit=500 /home/kr/archive/ user@host:/backup/
# Limited to ~500 KB/s
8. Set Up Passwordless rsync
$ ssh-keygen -t ed25519 -C "backup@olympos"
$ ssh-copy-id user@192.168.1.100
$ rsync -avzP /home/kr/ user@192.168.1.100:/backup/
# โ
no password
9. Exclude Files via a Pattern File
$ cat ~/.rsync-exclude
.git/
node_modules/
*.log
*.tmp
$ rsync -av --exclude-from=~/.rsync-exclude /project/ /backup/
10. Sync Only New Files (No Overwrites)
$ rsync -av --ignore-existing /source/ /dest/
# Skips files already present in dest
11. Verify with Checksums Instead of Time
$ rsync -avc /source/ /dest/
# Uses checksums โ slower but more accurate
12. Delete Only Files Removed from Source
$ rsync -av --delete --ignore-existing /source/ /dest/
# Only deletes files no longer in source, doesn't overwrite existing
13. Show Only the Summary
$ rsync -a --stats /source/ /dest/
14. Sync with a Custom SSH Key
$ rsync -avz -e 'ssh -i ~/.ssh/backup_key' /source/ user@host:/dest/
15. Pull Backup from a Server
$ rsync -avz --delete user@server:/var/www/ /backup/www/
Visual: How rsync Works
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ First rsync run โ
โ โ
โ Source Destination โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โ โ file1 โ โ โ โ
โ โ file2 โ โโโโโบ โ โ โ
โ โ file3 โ โ โ โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โ โ
โ All files copied โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Second rsync run โ
โ โ
โ Source Destination โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โ โ file1 โ skip โ file1 โ โ
โ โ file2' โ โโโโโบ โ file2 โ โ
โ โ file3 โ skip โ file3 โ โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โ โ
โ Only file2' transferred (file2 changed) โ
โ file1 and file3 are skipped โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Command | Purpose | Example |
|---|---|---|
rsync -av SRC DST | Local copy | rsync -av /home/kr/cli /home/kr/clibkp |
rsync -avz user@host:SRC DST | Remote โ local | rsync -avz user@192.168.1.100:/home/kr/cli /home/kr/clibkp |
rsync -avz SRC user@host:DST | Local โ remote | rsync -avzP /home/kr/cli user@host:/home/kr/cli |
rsync -av --delete SRC DST | Mirror + delete extras | rsync -av --delete /home/kr/cli user@host:/home/kr/cli |
rsync -avzP SRC user@host:DST | Resumable transfer | rsync -avzP /home/kr/cli user@host:/home/kr/cli |
rsync -avn SRC DST | Dry run | rsync -avn --delete SRC DST |
rsync -av --exclude=PAT SRC DST | Exclude patterns | rsync -av --exclude='.git' SRC DST |
rsync -avz --bwlimit=N SRC DST | Bandwidth limit | rsync -avz --bwlimit=1000 SRC DST |
ssh-keygen -t rsa -b 4096 -C "email" | Generate key pair | ssh-keygen -t rsa -b 4096 -C "me@x.com" |
ssh-copy-id user@host | Copy public key | ssh-copy-id user@remote_host |
Key takeaways:
rsynccopies and synchronizes files โ local or remote โ and only transfers changes-apreserves permissions, timestamps, symlinks, and ownership โ always use it for backups-vfor verbose,-zfor compression,-Pfor progress and resume--deletemakes the destination an exact mirror โ always dry-run first with-n- Trailing slash on the source matters:
SRC/= contents,SRC= the directory itself - The colon (
user@host:/path) marks a remote path -Plets you Ctrl+C and resume by running the same command- Set up SSH keys (
ssh-keygen+ssh-copy-id) for passwordless rsync - Use
--excludeto skip.git,node_modules,*.log, and other junk - Use
--bwlimitto avoid saturating a network link - For very large transfers,
rsyncbeatsscpbecause it resumes and only sends deltas
Remember: rsync is the most powerful file-copying tool on Linux. It’s faster than cp for repeated syncs because it only transfers what changed. It’s more reliable than scp for large transfers because it can resume. And with --delete, it turns any two directories into an exact mirror โ but that power comes with risk, so always dry-run with -n first. Watch your trailing slashes. Set up SSH keys. Exclude the junk. Master rsync, and backups, deployments, and file synchronization all become one command.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!