Lesson 95: rsync Command
In this lesson, you'll learn how to use the rsync command to copy and synchronize files and directories locally and remotely in Linux.
rsync (Remote Sync) is the most commonly used command for copying and synchronizing files and directories, both locally and remotely, on Linux/Unix systems.
With the help of the rsync command, you can copy and synchronize your data remotely and locally across directories, disks, and networks, perform data backups, and mirror between two Linux machines.
rsync command.Advantages of rsync
It offers several advantages, including:
- Efficient File Transfer -
rsyncuses a delta transfer algorithm, which means it only transfers the differences between the source and destination files, significantly reducing the amount of data transferred and making it efficient for syncing large files or directories. - Remote File Synchronization -
rsyncsupports both local and remote file transfers over SSH, which allows synchronization between local and remote systems or mirroring directories across multiple machines. - Incremental Backups -
rsyncis well-suited for incremental backups, as it creates and updates backups efficiently by transferring only new or modified files. - Preserves File Permissions -
rsynccan preserve various file attributes, such as permissions, ownership, timestamps, and symbolic links, which ensures that the copied files retain their original characteristics on the destination. - Bandwidth Control -
rsyncallows you to limit the bandwidth usage during file transfers, as it uses compression and decompression methods while sending and receiving data on both ends. - Faster -
rsynccan be faster thanscp(Secure Copy) for transferring files, especially when syncing large directories or when dealing with files that have already been partially transferred or exist on the destination.
rsync Command Syntax
# rsync [OPTIONS] SOURCE DESTINATION
rsync Command Options
| Option | Description |
|---|---|
-v |
Verbose output, displaying detailed information about the transfer |
-r |
Copy data recursively (does not preserve timestamps and permissions) |
-a |
Archive mode β copies recursively and preserves symbolic links, permissions, ownership, and timestamps |
-z |
Compress files during transfer to reduce network usage |
-h |
Human-readable β output numbers in a human-readable format |
-P |
Show progress during the transfer |
-e ssh |
Use SSH as the remote shell for the transfer |
--progress |
Display transfer progress for each file |
--include='PATTERN' |
Include only files matching the pattern |
--exclude='PATTERN' |
Exclude files matching the pattern |
--delete |
Delete files at the destination that no longer exist at the source |
--max-size='SIZE' |
Only transfer files equal to or smaller than the specified size |
--remove-source-files |
Automatically delete source files after a successful transfer |
--dry-run |
Simulate the transfer without making any changes |
--bwlimit=RATE |
Limit the I/O bandwidth during transfer |
-W |
Sync the whole file instead of just the changed blocks |
Install rsync in Linux
You can install the rsync package with your Linux distribution's package manager.
$ sudo apt install rsync # Debian, Ubuntu and Mint
$ sudo yum install rsync # RHEL/CentOS/Fedora and Rocky/AlmaLinux
$ sudo emerge -a sys-apps/rsync # Gentoo Linux
$ sudo apk add rsync # Alpine Linux
$ sudo pacman -S rsync # Arch Linux
$ sudo zypper install rsync # OpenSUSE
1. Copy/Sync a File Locally
To copy or sync a file locally, you can use the following command that will sync a single file on a local machine from one location to another location.
Here in this example, a file named backup.tar.gz needs to be copied or synced to the /tmp/backups/ folder.