rsync is a powerful, efficient tool for copying files and directories — and it’s especially useful for transferring files between locations (like across different machines or storage systems) while preserving permissions, timestamps, and even handling large files with minimal overhead.
✅ Basic Syntax
rsync [OPTIONS] SOURCE DESTINATION
🎯 Example: Transfer One File from Location A to Location B
Let’s say:
- Your file is at
/home/user/old_folder/myfile.txt - You want to copy it to
/mnt/backup/myfile.txt
➤ Simple Copy (Preserves all metadata)
rsync /home/user/old_folder/myfile.txt /mnt/backup/
This copies
myfile.txtto/mnt/backup/— if the destination folder doesn’t exist, rsync will create it.
🧩 Useful Options
1. --progress — Show progress during transfer
rsync --progress /home/user/old_folder/myfile.txt /mnt/backup/
2. --delete — Delete files in destination that don’t exist on source (useful for syncing)
⚠️ Not recommended for one-time transfers unless you’re doing a sync.
rsync --progress --delete /home/user/old_folder/myfile.txt /mnt/backup/
3. --no-preserve — Don’t preserve permissions, timestamps, etc. (rarely needed)
rsync --no-preserve /home/user/old_folder/myfile.txt /mnt/backup/
4. --backup / --backup-dir — Create backup copies (optional)
Use this if you want to keep a copy of the original file before overwriting.
rsync --backup --backup-dir=/mnt/backup.bak/ /home/user/old_folder/myfile.txt /mnt/backup/
🌐 Transfer Between Machines (SSH)
If you’re transferring over SSH (common use case):
rsync -avz -e "ssh -p 22" /home/user/old_folder/myfile.txt user@remote-host:/path/to/destination/
Breakdown:
-a— archive mode (preserves permissions, timestamps, symlinks, etc.)-v— verbose output-z— compress data during transfer-e "ssh -p 22"— specify SSH connection (replace22with port if needed)user@remote-host— target machine/path/to/destination/— destination path on remote machine
💡 Tip: Use
rsync -avzfor most cases — it’s the “default” good option for file transfers.
📌 Summary: Quick Commands
| Purpose | Command |
|---|---|
| Simple copy | rsync /source/file.txt /destination/ |
| Copy with progress | rsync --progress /source/file.txt /destination/ |
| Copy via SSH | rsync -avz -e "ssh -p 22" /source/file.txt user@host:/destination/ |
| Copy with backup | rsync --backup --backup-dir=/backup/ /source/file.txt /destination/ |
🚫 Common Mistakes
- ❌
rsync file.txt destination/— Wrong! You must specify the full source path. - ❌
rsync -r file.txt—rsyncdoesn’t work with single files like that. Usersync file.txt destination/. - ❌
rsync -r /path/—rsyncdoesn’t copy directories unless you specify the file inside.
✅ Bonus: Check if rsync is installed
which rsync
If you don’t see it, install it:
- Ubuntu/Debian:
sudo apt update && sudo apt install rsync
- CentOS/RHEL/Fedora:
sudo yum install rsync # CentOS/RHEL
sudo dnf install rsync # Fedora
🚀 Created with my self hosted AI. 1st one as well!
