How to Use Rsync Command in Linux

By admin

Rsync, short for remote sync, is a file transfer and synchronization tool that securely copies and synchronizes files between two directories. One must be a source and the other a destination, which may also be remote. It uses a delta-transfer algorithm that sends only the differences between the source and destination files or folders. Thus, it makes for a bandwidth-efficient tool and a sound choice for incremental data transfers.

Rsync is a perfect alternative for the scp command which is now deprecated due to vulnerability concerns. Rsync is widely used for offsite backups and mirroring.

By default, rsync comes pre-installed on modern Linux distributions, and therefore, no installation is required. In this article, we explore various ways you can use the rsync tool to transfer files and synchronize local and remote directories.

Rsync Command Syntax

Before going into how to use the rsync command, let’s start by reviewing the basic syntax.

The rsync utility expressions take the following form: 

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]
  • OPTIONThe rsync options.
  • SRC – Source directory.
  • DEST – Destination directory.
  • USER – Remote username.
  • HOST – Remote hostname or IP Address.

rsync provides a number of options that control how the command behaves. The most widely used options are:

  • -a, –archive, archive mode, equivalent to -rlptgoD. This option tells rsync to syncs directories recursively, transfer special and block devices, preserve symbolic links, modification times, groups, ownership, and permissions.
  • -z, –compress. This option forces rsync to compresses the data as it is sent to the destination machine. Use this option only if the connection to the remote machine is slow.
  • -P, equivalent to –partial –progress. When this option is used, rsync shows a progress bar during the transfer and keeps the partially transferred files. It is useful when transferring large files over slow or unstable network connections.
  • –delete. When this option is used, rsync deletes extraneous files from the destination location. It is useful for mirroring.
  • -q, –quiet. Use this option if you want to suppress non-error messages.
  • -e. This option allows you to choose a different remote shell. By default, rsync is configured to use ssh.

Basic Rsync Usage

The most basic use case of rsync is to copy a single file from one to another local location. Here is an example: 

rsync -a /opt/filename.zip /tmp/

The user running the command must have read permissions on the source location and write permissions on the destination.

Omitting the filename from the destination location copies the file with the current name. If you want to save the file under a different name, specify the new name on the destination part: 

rsync -a /opt/filename.zip /tmp/newfilename.zip

Conclusion

We have shown you how to use Rsync to copy and synchronize files and directories. There’s lots more to learn about Rsync at Rsync User’s Manual page.