Skip to main content

Module 9: Networking

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.

πŸ’‘
Note: You don't need to be a root user to run the rsync command.

Advantages of rsync

It offers several advantages, including:

  • Efficient File Transfer - rsync uses 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 - rsync supports both local and remote file transfers over SSH, which allows synchronization between local and remote systems or mirroring directories across multiple machines.
  • Incremental Backups - rsync is well-suited for incremental backups, as it creates and updates backups efficiently by transferring only new or modified files.
  • Preserves File Permissions - rsync can 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 - rsync allows 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 - rsync can be faster than scp (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.

Pro TecMint Β· Root Plan
This Article is for Root Members
Join Root to read the full article and unlock everything

Full Access to Every Article, Course & Certification Track

Join thousands of Linux professionals who use Pro TecMint to advance their careers.

Ad-free access to all premium articles
All courses: Learn Linux, Bash, Golang, Ubuntu and more
RHCSA, RHCE, LFCS & LFCA certification prep
New courses added every month
Private Telegram community & priority support
Root Plan
$8/mo
or $59/year billed annually
Save $37 with annual plan
Start Reading This Article in the Next 60 Seconds
Join Root Plan β†’