Sync New Changed Modified Files Rsync Linux
In this article
To sync only new, changed, or modified files with rsync on Linux, use the -u (update) flag combined with -a (archive) and -v (verbose), such as rsync -auv source/ destination/. This transfers files that are new on the source or have a newer modification time than their counterpart on the destination, skipping everything else.
What Rsync's Update Flag Actually Does
The -u or --update option tells rsync to skip files that already exist on the destination and have a modification time newer than or equal to the source file. In practice, this means:
- Files that exist only on the source are copied (new files).
- Files that exist on both sides are copied only if the source version is newer (changed or modified files).
- Files that are identical or where the destination is newer are left alone.
This is not the same as a full mirror. A mirror with --delete would also remove files on the destination that no longer exist on the source, and it would overwrite any destination file that differs, regardless of timestamps. The update flag is deliberately conservative: it never overwrites a newer destination file.
Why Use -u Instead of Just -a or -r
The plain archive flag -a (which implies -r for recursive, plus permissions, timestamps, and other metadata) will copy every file that differs in any way, even if the destination is newer. That is often what you want for a full backup, but it is wasteful when you only want to bring over recent changes. The -u flag adds a time check on top of that, so rsync skips files where the destination already has a newer or equal modification time.
For example, if you have a directory with 10,000 files and only 20 changed today, rsync -a would scan and potentially transfer all 10,000 (if any metadata differs), while rsync -au would only transfer those 20. The time savings can be substantial, especially over slow network links.
How to Choose the Right Rsync Command
The exact command depends on whether you want a one-way sync, a mirror, or just a quick copy of recent changes. Here are the common patterns:
- Copy only new and newer files:
rsync -auv source/ destination/ - Same, but also delete files on the destination that are no longer on the source:
rsync -au --delete source/ destination/(note: this still respects the update rule for existing files) - Include a dry run to see what would happen:
rsync -auvn source/ destination/ - Add compression for slow networks:
rsync -auz source/ destination/ - Show progress and human-readable sizes:
rsync -auh --progress source/ destination/
Always include a trailing slash on the source directory (source/) to copy the contents of the directory, not the directory itself. Without the slash, rsync creates a nested folder like destination/source/.
Common Problems and Misconceptions
One frequent mistake is assuming -u also checks file content. It does not. It only compares modification times. If a file was modified but its timestamp was manually set back (or preserved from an older copy), rsync will skip it. To force a content-based comparison, use -c (checksum) instead of or in addition to -u, but this is slower because it reads every file on both sides.
Another issue is that -u does not handle files that were deleted on the source unless you add --delete. If you want the destination to exactly match the source, you need both -u (to avoid overwriting newer files) and --delete (to remove extras). Be careful: --delete will remove files on the destination that you might still want, so test with --dry-run first.
Also note that rsync's default behavior with timestamps can be affected by filesystem differences. For example, FAT32 has a two-second timestamp granularity, so a file modified just now might appear older than it is. Using -c avoids this issue entirely.
Rsync vs. Other Sync Tools
Rsync is not the only way to sync files on Linux. Here is how it compares to common alternatives:
- cp -u: The GNU
cpcommand has a-uflag that copies only when the source is newer, but it does not handle recursive deletion, permissions, or hard links as elegantly as rsync. It is fine for simple copies. - unison: A bidirectional sync tool that tracks changes on both sides and can merge them. Rsync is one-way only (source to destination), so unison is better for two active copies.
- syncthing: A continuous, peer-to-peer sync daemon that runs in the background. Rsync is typically run manually or via cron, not as a real-time service.
- git: For source code, git tracks changes and merges, but it is not designed for large binary files or arbitrary directory trees. Rsync is more general.
For most one-way backup or mirror tasks, rsync remains the standard because it is fast, flexible, and available on virtually every Linux system by default.
Safety Tips and Best Practices
Before running any rsync command that modifies the destination, especially with --delete, follow these precautions:
- Always do a dry run first:
rsync -auvn --delete source/ destination/. Thenflag shows what would happen without changing anything. - Double-check the source and destination paths. A single misplaced slash or typo can cause data loss.
- Use
--backupto keep old versions of overwritten files:rsync -au --backup --backup-dir=/path/to/backups source/ destination/. - Test on a small sample directory before running on the full data set.
- If syncing over SSH, use
rsync -auz -e ssh source/ user@host:/destination/and consider using SSH keys to avoid password prompts.
Performance Considerations
Rsync's speed depends on several factors. The -u flag helps by skipping files that do not need updating, but rsync still has to stat every file on both sides to compare timestamps. For very large directories (millions of files), that metadata scan can take time. Options to speed it up:
- Use
--itemize-changes(-i) to see exactly what rsync is doing, which helps debug unexpected transfers. - Use
--partialto keep partially transferred files if the connection drops, avoiding a full restart. - Use
--inplaceto update files in place instead of creating a temporary copy, which can be faster for large files but risks corruption if the process is interrupted. - For local filesystems, consider using
--no-whole-file(default for local) to allow delta transfers, but note that rsync already does this automatically.
Network bandwidth is usually the bottleneck, so -z (compression) can help for text files but may slow down transfers of already-compressed data like images or videos.
Frequently Asked Questions
Does rsync -u also copy files that are the same size but different content?
No. The -u flag only compares modification times. If the source file has a newer timestamp, it is copied regardless of content. If the timestamps are equal or the destination is newer, it is skipped even if the content differs. To force content comparison, use -c (checksum), but this reads every file on both sides and is slower.
How do I sync only files modified in the last 24 hours?
Rsync does not have a built-in time filter, but you can combine it with find: find source/ -type f -mtime -1 -exec rsync -aR {} destination/ \;. The -R flag preserves the relative path. Alternatively, use --files-from with a list of files generated by find.
What is the difference between rsync -u and rsync -c?
-u (update) skips files where the destination has a newer or equal modification time. -c (checksum) compares file contents by computing a checksum on both sides, so it catches any difference regardless of timestamps. -c is more accurate but much slower because it reads every file.
Can I use rsync to sync files from a Windows machine to Linux?
Yes, but Windows does not have rsync natively. You can install WSL (Windows Subsystem for Linux) and run rsync there, or use a third-party port like cwRsync. The command syntax is the same, but be aware of path format differences (e.g., C:\path vs /mnt/c/path in WSL).
Why does rsync copy files that haven't changed?
This usually happens because the modification time or size differs slightly, even if the content is the same. For example, if you copied the destination with a tool that changed the timestamps, rsync will see a difference. Use -c to force content-based comparison, or check if you have the -t flag (preserve times) enabled, which is included in -a.
Is it safe to use rsync --delete with -u?
It is safe in the sense that rsync will not delete a file that is newer on the destination, because -u tells it to skip such files. However, it will delete files that exist on the destination but not on the source, regardless of their age. Always do a dry run first and back up important data.