Rsync Command in Linux: A Guide with Examples

Rsync Command in Linux A Guide with Examples

Rsync is a powerful and versatile tool used for synchronizing files and directories between two different locations in a Unix-based operating system. It is a simple, fast, and efficient way to keep files and folders up-to-date across different systems, which makes it a crucial tool for many system administrators and developers. This article provides an in-depth guide on how to use the Rsync command in Linux, including its syntax and various options, as well as some practical examples.


How to Install Rsync

Rsync is included in most Linux distributions by default, but in case it’s not installed, you can easily install it using the package manager. Here are the instructions for installing Rsync on different Linux distributions:

Install Rsync on Ubuntu and Debian:

You can install Rsync on Ubuntu and Debian by running the following command in your terminal:

sudo apt install rsync

Install Rsync on CentOS and Fedora:

You can install Rsync on CentOS and Fedora by running the following command in your terminal:

sudo yum install rsync

Rsync Command Syntax:

The basic syntax of the Rsync command is as follows:

For Local to Local:

rsync [OPTION]... [SRC]... DEST

Local to Remote:

rsync [OPTION]... [SRC]... [USER@]HOST:DEST

Remote to Local:

rsync [OPTION]... [USER@]HOST:SRC... [DEST]

Where:

  • OPTION refers to any optional parameters that you want to include in the Rsync command.
  • SRC is the source directory or file that you want to copy.
  • DEST is the destination directory or file where you want to copy the source.
  • USER – Remote username.
  • HOST – Remote hostname or IP Address.

Rsync Command Options

The Rsync command has several options that can be used to customize the synchronization process. Some of the most commonly used options are:

  • -a: This option stands for “archive” and ensures that the synchronization is performed in archive mode, which means that the source files and directories are copied recursively, preserving their permissions, timestamps, symbolic links, and other attributes.
  • -v: This option stands for “verbose” and displays detailed information about the synchronization process, including the names of the files and directories that are being copied.
  • -z: This option compresses the data that is being transferred, which speeds up the synchronization process and reduces network bandwidth usage.
  • --delete: This option deletes files and directories in the destination location that no longer exist in the source location.
  • -r: This option stands for “recursive” and enables the Rsync command to copy directories recursively, including all of their subdirectories and files.
  • -P: The -P option in Rsync is equivalent to the --partial and --progress options combined. When this option is used, Rsync displays a progress bar during the transfer, and keeps partially transferred files.
  • -e: The -e option in Rsync specifies the remote shell to be used for executing the Rsync command on the remote host. This allows for customizing the transfer process when the remote shell is different from the local host.

Basic Rsync Command Usage

Here is a basic example of using Rsync to copy files from one directory to another:

rsync -av /path/to/src /path/to/dest

In this example, the -a option stands for “archive” mode, which copies files and directories recursively, preserving their permissions, timestamps, symbolic links, and other attributes. The -v option stands for “verbose” and displays detailed information about the synchronization process, including the names of the files and directories that are being copied.


How to Synchronizing two directories

To synchronize two directories, use the following command:

rsync -avz /path/to/src /path/to/dest

This command will copy all files and directories from /path/to/src to /path/to/dest recursively, preserving their attributes and displaying verbose output.


How to Sync Data from/to a remote Machine

Rsync can also be used to synchronize files and directories between a local machine and a remote machine. Here is an example of using Rsync to copy files from a local machine to a remote machine:

rsync -avz /path/to/src user@remote.host:/path/to/dest

In this example, user is the username on the remote host and remote.host is the hostname or IP address of the remote machine. The -z option compresses the data that is being transferred, which speeds up the synchronization process and reduces network bandwidth usage.


How to Synchronizing a directory and deleting files in the destination that no longer exist in the source

To synchronize a directory and delete files in the destination that no longer exist in the source, use the following command:

rsync -avz --delete /path/to/src /path/to/dest

This command will copy all files and directories from /path/to/src to /path/to/dest recursively, preserving their attributes, and deleting any files or directories in /path/to/dest that no longer exist in /path/to/src.


How to Exclude Files and Directories

Rsync provides the --exclude option, which allows you to exclude certain files and directories from the synchronization process. Here is an example of using the --exclude option to exclude a directory from the synchronization:

rsync -av --exclude=directory_to_exclude /path/to/src /path/to/dest

In this example, the directory directory_to_exclude will be excluded from the synchronization process. You can also use the --exclude option to exclude files with a specific pattern, such as *.log or *.tmp.

The --include option works similarly to --exclude, but instead of excluding files and directories, it only includes files and directories that match the specified pattern. For example:

rsync -av --include='*.jpg' --exclude='*' /path/to/src /path/to/dest

In this example, only *.jpg files will be included in the synchronization process, while all other files and directories will be excluded.

In addition to the --exclude and --include options, Rsync also provides the --filter option, which allows you to specify more complex exclusion and inclusion rules. For example:

rsync -av --filter='- *.log' /path/to/src /path/to/dest

In this example, the --filter option is used to exclude all *.log files from the synchronization process.

These options allow you to fine-tune the synchronization process and ensure that only the files and directories that you want to copy are actually copied.


Conclusion:

The Rsync command in Linux is a powerful tool that allows you to synchronize files and directories between two different locations. With its various options and flexible syntax, you can customize the synchronization process to meet your specific needs. Whether you are a system administrator, developer, or just someone who needs to keep their files and directories up-to-date, the Rsync command is a must-have tool in your toolkit.

Got questions or suggestions on the article? Don’t hesitate to leave a comment below. Your feedback is valuable to us and helps improve the content quality. And don’t forget to share this article with others who might find it useful. Looking forward to hearing from you!

If our tutorials helped you, please consider buying us a coffee. We appreciate your support!

Thank you for your support.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top