With the help of the rsync command in Linux, you can efficiently copy and sync files from a local system to a remote server and vice versa. From links to groups, devices, and permissions, the command can help copy everything.
The remote sync, popularly known as the rsync command, is a utility that helps in copying and synchronizing files or directories within a local machine or even between remote servers. Consuming comparatively less bandwidth, rsync
offers the fastest transfer rate.
Although there are other methods like scp (Secure Copy) for dealing with similar purposes, rsync
comes out as a more efficient solution, all thanks to its nature of using a remote update protocol. Moreover, it allows users to employ SSH protocol and keep the data safe during the transfer.
In this article, we’ll look at the basic and advanced uses of rsync
command, but before that, let’s understand the Syntax of the rsync command and learn how to get the package installed.
rsync Command in Linux: Syntax
The basic structure of the rsync command in Linux looks something like this:
$ rsync [OPTIONS] [SOURCE DESTINATION]
Or,
$ rsync [OPTION]... SRC [SRC]... DEST

Installing rsync Command
Getting the rsync package installed is a pretty straightforward task. All you need to do is launch the Terminal by using the Ctrl+Alt+T key combination and then run the command according to the system you’re using.
On Arch Linux
$ pacman -S rsync
On openSUSE
$ sudo zypper install rsync
On Debian/Ubuntu & Mint
$ sudo apt-get install rsync
On Gentoo
$ emerge sys-apps/rsync
On Fedora/CentOS
$ sudo dnf install rsync
Using the rsync Command in Linux
Now that you know how to install the rsync
package in UNIX systems, it’s time we walk through the uses of this command-line utility in detail.
Syncing or Copying a File/Directory on a Local Computer Using rsync Command in Linux
To copy/sync a single file on a local machine, pass the -zvh
flag alongside the rsync command. At the same time, if you wish to copy/sync multiple files, use the -avzh
flag followed by the desired directory.
For instance, if you’ve got a single file named newfile.tar that needs to get copied or synced to /tmp/newbackups
, run the following command:
$ rsync -zvh newfile.tar.gz /tmp/newbackups/

On the other hand, if there are multiple files located in any directory, say /root/newpkgs
, and you want all the files to get synced to /tmp/newbackups
, run the following command:
$ rsync -avzh /root/newpkgs /tmp/newbackups

If the destination provided doesn’t exist, the rsync command in Linux will automatically create one for completing the process.
Syncing or Copying a File/Directory from a Local Computer to a Remote Server
Suppose you want to sync a directory named localbackupsmain containing localbackups to a remote server having IP address 192.168.0.132, launch the Terminal, and run the following command:
$ rsync -avzh /root/localbackupsmain root @192.168.0.132 :/root

Now, if there is a directory /root/localbackupsmain
on a remote server 192.168.0.132, and you want to transfer those files to the /tmp/newfiles
directory on your local computer, run the following command:
$ rsync -avzh [email protected] :/root/localbackupsmain /tmp/newfiles

Using SSH with rsync Command
You can use the SSH (secure shell) protocol alongside the rsync command to secure the data transfer. The SSH protocol encrypts the entire data-syncing process so that no one can read it over the wire.
Specifying a protocol with the rsync
command is simple. Just use the -e
flag and the name of the desired protocol. Let’s understand it better with an example.
Running the following code will copy a file from a local server and sync the same to a remote server with SSH
$ rsync -avzhe ssh newfile.tar.gz [email protected] :/newbackups/

To copy/sync a file from a remote server to the local machine with SSH protocol, use the code in the following format:
$ rsync -avzhe ssh [email protected] :/root/python-ks.cfg /tmp

Selecting the Desired Files to Include or Exclude
The rsync command in Linux is very dynamic. It allows users to pass the -include/exclude
flag and specify the files they want to be transferred.
Suppose you want to include all the files in the directory /root/newbackups
that starts with A and exclude the rest, run the following command:
$ rsync -avze ssh -include 'A*' --exclude '*' [email protected] :root/newbackups

Displaying Data Transfer Progress
Do you want your system to display the data transfer progress from one system to another? In that case, you’ll have to supply the -progress
option
$ rsync -avzhe ssh --progress /root/newpkgs [email protected] :root/newpkgs

Deleting an Existing File
If you want to delete an existing file or directory before initiating the sync process, use the -delete
option.
Setting the Maximum Size of File that you Want to Transfer with rsync Command in Linux
Specifying the maximum size of the file before initiating a transfer is simple. It only takes the usage of -max-size
option followed by the desired number. The rsync
command will then permit file transfer up to the provided limit.
$ rsync -avzhe ssh -max-size='150k' [Source Destination]

Running the command mentioned above will ensure that the files equal or smaller to 150k get transferred.
Deleting Source Files After Transfer
Once you’ve transferred file(s) from one server to the other, you might wish to have the ones on the source deleted. Luckily, you can use the rsync command to do that automatically. Simply pass the -remove-source-files
options, and you’re good to go.
For Example:
$ rsync -remove-source-files -zvh ssh newfile.tar.gz [email protected] :/newbackups/

Setting the Bandwidth Limit while Using rsync Command in Linux
Similar to how you can use the -max-size
option to limit the maximum file size, employing the -bwlimit
flag will restrict the I/O bandwidth.
Run the code in the following format:
$ rsync --bwlimit=100 -avzhe ssh [Source Destination]
Common Options that are Used with rsync Command in Linux
-v
: Using this option will print the verbose
-a
: The -a
flag signifies archive mode. It allows the system to copy files recursively while preserving symbolic links, file permissions, user groups, and timestamps.
-z
: It helps in compressing the file data.
-r
: Passing the -r
option will copy the data recursively but without preserving symbolic links, file permissions, user groups, and timestamps.
-h
: It prints digits in a human-readable format.
Do you want to access the entire list? Well then, use the --help
flag
Input:
$ rsync --help
Output:

And this is how you use the rsync command in Linux. In this article, we’ve decoded the rsync command in every possible manner. From installing in the package to using it for syncing data in various scenarios, you’ll get to learn everything about this command line in detail.
If this guide helped you, please share it.