How to use rsync command to copy data locally and over SSH

Copying files from one location to another is a pretty basic task and can be easily achieved by using the cp command. However, sometimes, you have to do a little more. This is where the rsync command comes especially when you are transferring the larger files. Rsync (Remote Sync) is a Linux based command-line tool that can be used to sync and copy files between local and remote servers. It allows synchronization of files between two directories on the same computer, between the directories in two different computers on the same network, between two directories on the remote system.

The major advantage of rsync is that instead of blindly copying the whole content, instead it analyze and transfer only the differences between the source and destination possibly saving the system resources and bandwidth. Some other advantages of rsync are as follows:

  • Compresses the files before transferring
  • Can view progress bar while transferring data
  • Copying process can be paused and resumed later
  • Allows transfer via SSH connection

In this article, we will explain how to use rsync for copying data in Linux. We will use Debian 10 for describing the procedure mentioned in this article.

Installation of Rsync

Rsync is installed in most of the Linux operating systems by default. However, If it is not installed, then you can install it by following the below steps:

Launch the Terminal application in your system by going into the Activities tab in the top left corner of your Debian desktop. Then in the search bar, type terminal. When the Terminal icon appears, click on it to launch it.

Now execute the below command in Terminal to install rsync utility:

$ apt install rsync

Using Rsync

Every rsync command starts with rsync followed by an option in which you can specify various parameters. Rsync provides a number of options that give you the control to shape the transfer specifications. Then there is a source and destination that depends on whether they are a remote or local machine.

The basic syntax of rsync is:

$ rsync [option] [source] [destination]

Some of the general options used with rsync commands:

  1. -v : verbose
  2. -a : archive mode, it allows copying files recursively and preserves symbolic links, file permissions, user & group ownerships and timestamps
  3. -z : compresses data
  4. -h : human-readable, it output numbers in a human-readable format
  5. –max-size=SIZE, transfer files with the specified minimum SIZE
  6. –min-size=SIZE, transfer files with the specified maximum SIZE

Now we will explain some examples of using rsync with these options.

Copy files and directories to the local server

Copy files locally

You can copy single or multiple files from a source directory to the destination directory on the same machine. The example would be to copy all the .jpg files from the ~/Downloads directory to the ~/Documents directory using the following command:

$ rsync -zvh /home/tin/Downloads/*.jpg /home/tin/Documents

Copy data locally with rsync

After running the above command, all .jpg files in the source directory /home/tin/Downloads will be copied to destination directory /home/tin/Documents.

In the above example, we have used the options like -z for compression, -v for verbose output and -h for human-readable output.

Copy directories locally

Similar to files, you can also copy the entire folder and the files contained in a directory using rsync command. An example would be to copy all the files from the ~/Downloads directory to the ~/Documents directory using the following command:

$ rsync -zavh /home/tin/Downloads/ /home/tin/Documents

Copy directories locally

 

Copy files to a remote server

You can copy single or multiple files from a source directory to destination directory on a remote machine. For instance, to copy the files from local to a remote machine, use the following command syntax:

$ rsync [options] [local path] [user]@[remote server]:[remote path]

Copying Files Based on Maximum Size

You can also specify the maximum size of the file that can be copied from source to destination using rsync. To do so, use the following command syntax:

$ rsync --max-size=”” [source] [destination]

An example would be to copy files with a maximum size of 4000K from source ~/Downloads to destination ~/Documents directory. In this case, files with a size larger than 4000k will not be copied to the destination.

$ rsync --max-size=4000k /home/tin/Downloads/*.iso /home/tin/Documents/

Restrict file size when copying files with rsync

Copying Files Based on Minimum Size

Similarly, you can also specify the minimum size that can be copied from source to destination using rsync. To do so, use the following command syntax:

$ rsync --min-size=”” [source] [destination]

An example of this would be to copy files with a maximum size of 2M from source ~/Downloads to destination ~/Documents directory. Files with size less than 2M will not get copied using rsync.

$ rsync --min-size=2M /home/tin/Downloads/*.iso /home/tin/Documents/

Copying Files Based on Minimum Size

View progress

You can also view progress while copying files using rsync. Add –progress flag following the rsync command as shown below:

$ rsync --progress [source] [destination]

An example would be to view the progress while copying a file from source ~/Downloads to destination ~/Documents directory:

$ rsync --progress /home/tin/Downloads/VTS_03_1.VOB /home/tin/Documents

View copy progress in rsync

Exclude files

You can also exclude certain files while copying the files from source to destination using rsync. An example would be to exclude all .pdf files while copying files from source ~/Downloads to destination ~/Documents directory.

rsync -zavh --exclude="*.pdf" /home/tin/Downloads/ /home/tin/Documents

Exclude files from being copied by rsync

By entering the following command, all files will be copied from source to destination except .pdf files.

That is all there is to it! We have explained the basic usage of rsync utility for copying files between different directories locally and remotely. We have also explained a few common and useful rync options. There are so many options other than these that you can use to control every aspect of the transfer behavior. To use those options, see the rsync man pages.