Mastering rsync: Practical Linux Examples for Fast, Reliable File Sync
This guide walks through installing rsync on CentOS and demonstrates nine real‑world examples—including local directory sync, preserving timestamps, remote transfers, avoiding overwrites, progress monitoring, deletion, include/exclude patterns, and size limits—complete with commands, output, and key options.
rsync is a powerful utility for synchronizing files and directories between local and remote systems. It can operate on both local and remote servers, making it ideal for backups and deployments.
Installation
On CentOS, install rsync with:
[root@localhost ~]# yum -y install rsyncExample 1: Sync two local directories
Use rsync -zvr /var/log/ /root/temp/ to copy files from /var/log/ to /root/temp/ with compression, verbose output, and recursion.
[root@localhost ~]# rsync -zvr /var/log/ /root/temp/
sending incremental file list
btmp
... (output truncated)
sent 516,136 bytes, received 605 bytes, 1,033,482.00 bytes/sec
total size is 5,451,242 speedup is 10.55-z enables compression.
-v prints detailed information.
-r enables recursive copy.
Example 2: Preserve timestamps with -a
The -a (archive) option combines recursion, symbolic‑link preservation, permission retention, timestamp preservation, and owner/group preservation.
[root@localhost ~]# rsync -azv /var/log/ /root/temp/
... (output shows timestamps retained)Example 3: Sync from local to remote
Both machines must have rsync installed. Use:
[root@localhost ~]# rsync -avz /root/temp/ [email protected]:/root/temp
[email protected]'s password:
... (files transferred)Example 4: Sync from remote to local
Reverse the source and destination:
[root@localhost ~]# rsync -avz [email protected]:/root/temp /root/temp
[email protected]'s password:
... (files received)Example 5: Avoid overwriting newer files with -u
The -u (update) flag skips files that are newer on the destination.
# rsync -avzu [email protected]:/root/temp /root/
... (only older files are copied)Example 6: Show progress with --progress
Add --progress to display a live transfer progress bar.
[root@localhost ~]# rsync -avz --progress /root/temp/ [email protected]:/root/tempExample 7: Delete extraneous files with --delete
Use --delete to remove files in the destination that no longer exist in the source.
# rsync -avz --delete /root/temp [email protected]:/root
... (files not present in source are deleted)Example 8: Include and exclude patterns
Specify patterns to include or exclude particular files or directories.
[root@localhost ~]# rsync -avz --include 'P*' --exclude '*' [email protected]:/var/lib/rpm/ /root/temp/Example 9: Limit transferred file size
Use --max-size (or --min-size) to restrict files by size.
[root@localhost ~]# rsync -avz --max-size='1M' [email protected]:/var/lib/rpm/ /root/temp/These examples cover common rsync options—compression, verbosity, recursion, archive mode, update, progress, deletion, pattern matching, and size limits—providing a practical reference for reliable file synchronization and backup on Linux systems.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
