When using rsync -avh
, the command combines several flags that affect how file transfers occur:
-a (archive mode)
-v (verbose)
-h (human-readable)
The crucial point is that -a
implies -t
(preserve modification times), which affects rsync's decision-making process about whether to overwrite files.
rsync uses a sophisticated algorithm to determine whether to transfer a file. Here's what happens when a file exists at the destination:
- Compares file sizes first (quick check)
- If sizes match, compares modification timestamps
- If timestamps differ, performs a checksum comparison
Example scenario where existing files might be overwritten:
# Source file (newer)
-rw-r--r-- 1 user user 1024 Jun 10 15:30 source/file.txt
# Destination file (older)
-rw-r--r-- 1 user user 1024 Jun 9 14:20 dest/file.txt
# Running:
rsync -avh source/ dest/
In this case, file.txt would be overwritten because the source file is newer.
Several options modify how rsync handles existing files:
--ignore-existing # Never overwrite existing files
--update (-u) # Skip newer files on receiver
--checksum (-c) # Force checksum comparison
--size-only # Compare only sizes
--backup # Make backups of overwritten files
Always test with --dry-run
first to see what would happen:
rsync -avh --dry-run source/ dest/
This outputs what files would be transferred without actually making changes.
When dealing with configuration files where you might have local modifications you want to preserve:
rsync -avh --ignore-existing configs/ /etc/
This ensures only new config files are copied, never overwriting existing ones.
The default timestamp comparison is fast but can sometimes lead to unnecessary transfers. For large files, consider:
rsync -avh --checksum large_files/ backup/
This performs a slower but more accurate checksum comparison to verify if files truly differ.
When using rsync -avh
(archive mode, verbose, human-readable), the command will:
- Compare source and destination files using checksums
- Only transfer files that differ between source and destination
- Overwrite existing files when they're different from source
- Preserve permissions, ownership, and timestamps
Consider this common directory structure:
source_dir/ ├── config.json └── app.js dest_dir/ ├── config.json └── old_script.js
Running:
rsync -avh source_dir/ dest_dir/
Here's what happens:
- config.json will be overwritten if different
- app.js will be copied (new file)
- old_script.js remains untouched
For more granular control:
# Skip newer files on receiver rsync -avh --ignore-existing source/ dest/ # Backup overwritten files (appends ~) rsync -avh --backup source/ dest/ # Dry run to see what would happen rsync -avh --dry-run source/ dest/
Always verify with:
rsync -avh --stats source/ dest/
This shows detailed transfer statistics including number of files overwritten.
If files aren't overwriting as expected:
- Check file permissions
- Verify no trailing slashes are missing
- Test with
--verbose
to see decision-making
Remember that rsync uses size and modification time checks by default. For absolute certainty, use --checksum
for full file comparison.