rsync: The Smart File Transfer Tool
rsync is your go-to solution for efficiently copying files and directories. Unlike regular copy commands, rsync only transfers the parts of files that have changed, making it incredibly fast for subsequent transfers.
Our Recommended Command
Section titled “Our Recommended Command”rsync -ahrvP source/ destination/Understanding Each Flag
Section titled “Understanding Each Flag”The Perfect Combination: -ahrvP
-a(Archive): Preserves everything important- File permissions
- Ownership
- Timestamps
- Symbolic links
- Special and device files
-h(Human-readable): Shows sizes in KB, MB, GB-r(Recursive): Copies directories and their contents-v(Verbose): Shows what’s happening-P(Progress): Shows transfer progress and enables resume
Real-World Examples
Section titled “Real-World Examples”Local Backups:
# Backup Documents folderrsync -ahrvP ~/Documents/ /media/backup/Documents/
# Backup multiple directoriesrsync -ahrvP ~/Documents/ ~/Pictures/ ~/Videos/ /media/backup/Remote Transfers:
# Copy to remote serverrsync -ahrvP ~/local/files/ user@server:/remote/path/
# Copy from remote serverrsync -ahrvP user@server:/remote/files/ ~/local/path/
# Using custom SSH portrsync -ahrvP -e "ssh -p 2222" ~/files/ user@server:/remote/path/Smart Backups:
# Sync and delete extra files in destinationrsync -ahrvP --delete ~/source/ /backup/
# Exclude certain filesrsync -ahrvP --exclude '*.tmp' --exclude 'cache/' ~/source/ /backup/Pro Tips
Section titled “Pro Tips”Trailing Slashes Matter:
# With trailing slash - copies contents of 'source'rsync -ahrvP ~/source/ /destination/
# Without trailing slash - copies 'source' directory itselfrsync -ahrvP ~/source /destination/Dry Run First:
# Test what will happen without making changesrsync -ahrvP --dry-run source/ destination/Resume Interrupted Transfer:
# -P flag allows resuming if connection dropsrsync -ahrvP user@server:/large/file ~/local/# If interrupted, run the same command to resumeCommon Use Cases
Section titled “Common Use Cases”Maintaining a Mirror:
rsync -ahrvP --delete source/ destination/Daily Backup Script:
#!/bin/bashrsync -ahrvP \ --exclude '*.tmp' \ --exclude 'node_modules/' \ ~/important-files/ \ /backup/$(date +%Y-%m-%d)/Remote Backup with Compression:
rsync -ahrvPz ~/large-files/ user@server:/backup/# -z adds compression for slow connectionsTroubleshooting Tips
Section titled “Troubleshooting Tips”- Use
--dry-runbefore important transfers - Add extra
vflags (-vvor-vvv) for more verbose output - Check permissions if transfers fail
- Use
--statsfor detailed transfer statistics - Add
--progressfor individual file progress
Prerequisites:
- rsync installed on both source and destination systems
- SSH access for remote transfers
- Sufficient disk space
- Appropriate read/write permissions