Table of contents
1.
Introduction
2.
What is Rsync Command?
3.
When to Use Rsync
4.
Syntax with Proper Example in Linux
5.
How It Works: Complete Working Process of Command
5.1.
Options Available in Rsync Command in Linux
5.2.
Examples
5.2.1.
Using Rsync as a List Command
5.2.2.
Copy/Sync Files and Directory Locally
5.2.3.
Rsync Using SSH
5.2.4.
Rsync with Particular File Permissions
5.2.5.
Rsync with --ignore-existing Option
5.2.6.
Show Progress During Transfer
5.2.7.
Automatically Delete Files from Local-Host After Successful Transfer
5.2.8.
Delete the Files That Have Been Deleted on the Local-Host
5.2.9.
Performing a Dry Run with Rsync
6.
Edge Cases We May Face
6.1.
Things to Keep in Mind
7.
Frequently Asked Questions
7.1.
What does the -z option do in rsync?
7.2.
Can rsync resume an interrupted transfer?
7.3.
How can I exclude certain files from being transferred?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

rsync command in linux

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In the realm of Linux, the rsync command stands as a versatile tool for data transfer and synchronization. It's a powerful utility that offers efficiency and flexibility, making it a preferred choice for backup operations, mirroring data, and for providing a semblance of security while transferring files over the network.

rsync command in linux

In this blog, we will learn about rsync command in linux. We will begin with the introduction and later cover how it works, and few examples of commands. 

What is Rsync Command?

rsync is an abbreviation for "remote synchronization." It is a utility for efficiently transferring and synchronizing files across computer systems, by checking the timestamp and size of files. It is commonly used for backups and mirroring and as an improved copy command for everyday use.

When to Use Rsync

The rsync command is particularly useful when you need to:

  • Backup and synchronize files between computers.
     
  • Minimize data transfer by only copying the changes made.
     
  • Ensure that files have been copied identically, with the option to verify checksums.

Syntax with Proper Example in Linux

The basic syntax of the rsync command is as follows:

rsync [options] source destination


For example, to copy files from a local directory to a remote server, you might use:

rsync -avz /local/directory/ user@remote:/remote/directory/


This command will archive (-a) the files, meaning it will maintain the permissions, ownership, and timestamps, compress (-z) the files during the transfer, and increase verbosity (-v) to provide detailed information about the transfer process.

How It Works: Complete Working Process of Command

rsync works by using an algorithm that minimizes the amount of data copied by only moving the portions of files that have changed. The first time you run rsync, it will copy the entire content between the source and the destination. On subsequent runs, it will only transfer the changed blocks or bytes to the destination, which makes the transfer very fast.

Options Available in Rsync Command in Linux

rsync comes with a plethora of options that can be used to customize its behavior. Some of the most commonly used options include:
 

  • -a (archive) - This option preserves the files' permissions, ownership, and timestamps.
     
  • -z (compress) - Compress file data during the transfer.
     
  • --delete - Delete extraneous files from the destination directory.
     
  • -r (recursive) - Recursively copy entire directories.
     
  • --exclude - Exclude files that match a pattern.

Examples

Using Rsync as a List Command

rsync -av --dry-run /source/directory/


This command will list all the files that are to be copied without actually copying them (--dry-run).

Copy/Sync Files and Directory Locally

rsync -av /source/directory/ /destination/directory/


This command will copy files from the source to the destination directory on the local machine.

Rsync Using SSH

rsync -avz -e ssh /source/directory/ user@remote:/destination/directory/


This command will sync files over SSH for security.

Rsync with Particular File Permissions

rsync -avz --chmod=755 /source/directory/ /destination/directory/


This command will set the permission of all transferred files to 755.

Rsync with --ignore-existing Option

rsync -avz --ignore-existing /source/directory/ /destination/directory/


This command will skip updating files that already exist on the destination.

Show Progress During Transfer

rsync -avz --progress /source/directory/ /destination/directory/


This option will show the progress of the transfer.

Automatically Delete Files from Local-Host After Successful Transfer

 

rsync -avz --remove-source-files /source/directory/ /destination/directory/


This command will delete files from the source after copying.

Delete the Files That Have Been Deleted on the Local-Host

rsync -avz --delete /source/directory/ /destination/directory/


This will delete files in the destination directory that no longer exist in the source directory.

Performing a Dry Run with Rsync

rsync -avz --dry-run /source/directory/ /destination/directory/


This command simulates the transfer without making any changes.

Edge Cases We May Face

When using rsync, you might encounter situations such as file permissions not being correctly copied over, or symbolic links not being handled as expected. To handle these, you may need to use additional options like --perms for permissions and --links for symbolic links.

Things to Keep in Mind

Always perform a dry run (--dry-run) before the actual transfer to ensure the expected files are being copied.

Be cautious with the --delete option, as it can remove files unexpectedly if not used correctly.

When transferring files over a network, use the -z option to compress files during the transfer, saving bandwidth and time.

Frequently Asked Questions

What does the -z option do in rsync?

The -z option compresses file data during the transfer, which can speed up the transfer process, especially over slower network connections.

Can rsync resume an interrupted transfer?

Yes, using the -P option with rsync allows for the resumption of interrupted transfers.

How can I exclude certain files from being transferred?

The --exclude option allows you to specify patterns of files that rsync should not transfer.

Conclusion

The rsync command is a robust tool for file synchronization, offering a wide range of options to tailor to various data transfer needs. Whether you're performing backups, mirroring data, or just transferring files, rsync provides a reliable and efficient way to get the job done. With its ability to minimize data transfer by only copying the changes, it stands out as an essential tool for system administrators and users alike.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass