How to Use Rsync Command for Secure Data Transfer on Linux

The rsync command is one of the most powerful tools in Linux for efficient file synchronization and transfer. It is widely used by system administrators, developers, and anyone who needs to back up, copy, or mirror data between local and remote systems. rsync works by comparing the files at the source and destination, transferring only the changes, which makes it much faster than traditional file copying methods.

Whether you’re synchronizing directories, setting up automated backups, or transferring files between remote systems securely, rsync offers unmatched flexibility and performance.

rsync Command in Linux

This article explores rsync command features and usage, covering common use cases and providing examples to optimize file synchronization. From basic syntax to advanced options, you’ll learn how to efficiently manage files and improve data transfer.

Prerequisite

Before diving into the rsync command, it’s recommended that you have:

  • A basic understanding of Linux commands and the terminal.
  • Access to a Linux system with rsync installed (most distributions come with it pre-installed).
  • Familiarity with concepts like file paths and permissions, especially for remote transfers.

rsync Command Syntax

The basic syntax of rsync command in Linux is as follows:

rsync [options] source destination
  • source: The file or directory you want to copy or synchronize.
  • destination: The location where the file or directory should be copied to (local or remote).

Check Out | How to Use the grep Command in Linux for Powerful Searches

rsync Command Options in Linux

rsync command has several options that allow users to customize how files are transferred and synchronized. Below are some of the most commonly used options:

OptionDescription
-aArchive mode (preserves symbolic links, permissions, timestamps, etc.)
-vVerbose output (show details of the process)
-zCompress files during transfer to save bandwidth
-rRecursive mode (for directories)
-uSkip files that are newer on the destination
-eSpecify the remote shell (e.g., SSH) for remote transfers
-nDry-run (simulation without actually transferring files)
--deleteDelete files from the destination that are no longer in the source
--progressShow progress during the transfer
--excludeExclude files or directories from the transfer
-PCombination of --partial and --progress for resuming interrupted transfers

Install rsync on Linux

To install Rsync, you can use the following commands based on your Linux distribution. Rsync is typically available in the official repositories and may already be pre-installed on your system. If not, you can easily install it using the appropriate command for your distribution.

Here’s how you can install Rsync on different Linux distributions:

Install rsync Ubuntu and Debian:

Rsync is usually available in the default package repositories for Ubuntu and Debian-based systems. You can install it using the apt package manager.

To install Rsync on Ubuntu or Debian:

sudo apt update
sudo apt install rsync

Install rsync on FreeBSD

On FreeBSD, Rsync can be installed using the pkg package manager, which is the default package management tool for FreeBSD.

To install Rsync on FreeBSD:

sudo pkg install rsync

Install rsync on Arch Linux:

On Arch Linux, you can install Rsync from the official Arch repositories using the pacman package manager.

To install Rsync on Arch Linux:

sudo pacman -S rsync

Install rsync on Fedora, AlmaLinux, Rocky Linux, and VxLinux:

These distributions, being RHEL-based, use the dnf package manager (or yum , on older versions,) to manage packages. Rsync is available in the default repositories.

To install Rsync on Fedora, AlmaLinux, Rocky Linux, or VxLinux:

sudo dnf install rsync

Install rsync on CentOS:

CentOS, being another RHEL-based distribution, also uses the dnf or yum package manager, depending on the version. You can install Rsync using either.

To install Rsync on CentOS:

sudo yum install rsync  # For CentOS 7 or older
sudo dnf install rsync  # For CentOS 8 and newer

In most cases, Rsync should already be installed by default on many of these distributions, so you can verify its installation with:

rsync --version

Check Out | Sed Command Tutorial for Linux: Enhance Your Text Processing Skills

Rsync Command in Linux Examples

We will explore several basic rsync command examples to help you get a solid understanding of how to use the rsync command in Linux effectively. Each example will include an explanation to help clarify its usage.

  • Example 1: Syncing Files Locally
rsync -av dir1/ dir2/

This command synchronizes the contents of dir1 to dir2, preserving the directory structure, permissions, and timestamps. The -a flag is for “archive” mode, and the -v flag ensures verbose output.

Output:

sending incremental file list
file1.txt
file2.txt
sent 1024 bytes  received 2048 bytes  5120.00 bytes/sec
total size is 8192  speedup is 2.67
  • Example 2: Copying Files to a Remote Server Over SSH
rsync -avz /local/dir username@remote:/remote/dir

This command will copy files from the local system to a remote server using SSH for secure communication. The -z flag compresses the files during transfer to save bandwidth.

sending incremental file list
file1.txt
file2.txt
sent 1024 bytes  received 2048 bytes  5120.00 bytes/sec
total size is 8192  speedup is 2.67

Output:

sending incremental file list
file1.txt
file2.txt
sent 1024 bytes  received 2048 bytes  5120.00 bytes/sec
total size is 8192  speedup is 2.67
  • Example 3: Excluding Files During Sync
rsync -av --exclude 'tmp/' /source/ /backup/

This command will sync the contents of /source/ to /backup/, excluding the tmp/ directory from the transfer. The --exclude flag is used to filter out specific files or directories.

Output:

sending incremental file list
file1.txt
file2.txt
sent 1024 bytes  received 2048 bytes  5120.00 bytes/sec
total size is 8192  speedup is 2.67
  • Example 4: Synchronizing Remote Directories Using SSH
rsync -avz -e ssh /local/dir username@remote:/remote/dir

This command uses SSH (-e ssh) to securely transfer files between the local and remote system, compressing files during transfer (-z) and displaying the details of the operation (-v).

Output:

sending incremental file list
file1.txt
file2.txt
sent 1024 bytes  received 2048 bytes  5120.00 bytes/sec
total size is 8192  speedup is 2.67
  • Example 5: Dry Run for a Simulation
rsync -avn /source/ /destination/

The -n option performs a dry-run simulation of the transfer. It shows you what files would be copied without actually performing the transfer. This is useful for verifying your command before executing it.

Output:

sending incremental file list
file1.txt
file2.txt

Conclusion

The rsync command in Linux is a versatile and essential tool for file synchronization, backups, and transfers in Linux. It is especially useful for those working with large datasets or remote systems, providing features like compression, incremental backups, and secure transfers over SSH. By mastering rsync command, you can streamline your workflow, automate backups, and ensure that data is always in sync across systems.

Leave A Comment