Copy Data with rsync on Ubuntu

Copy Data with rsync on Ubuntu

Rsync (Remote sync) is a useful synchronization tool for copying and synchronizing files and directories in local and remote Linux systems. You can synchronize files between two directories on the same computer, between the directories in two systems on the same network and between two directories on the remote system.

In this article, I will describe some common use cases for rsync in Linux.

Installing Rsync on Ubuntu

Rsync is pre-installed with some of the Linux distributions. If it is not pre-installed, you can install it using the below command:

sudo apt install rsync

Using Rsync

The basic syntax of rsync is:

# rsync [options] [source] [destination]

In options, you can specify a variety of parameters.

Copy files locally

To copy all the files from a source directory to the destination directory, enter the below command:

rsync /home/tin/Documents/*.odc /home/tin/Desktop/data

Copy files locally with Rsync

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

Copy files from local to a remote system

Rsync can copy files over the network, all you need is the SSH login of the remote server or desktop.

Example:

rsync -e ssh /var/www/ root@192.168.1.100:/var/www/

This command copies the content of the local /var/www folder to the folder /var/www of the system on IP 192.168.0.100. It uses the user ‘root’ to log into the remote system.

Copying Files Based on Size

You can also specify the maximum size of the file that can be copied using rsync. Let’s say, in this case, I am specifying the maximum size to 0.2K. Files with size larger than this will not be copied to /home/tin/Desktop/data.

rsync --max-size=0.2k /home/tin/Documents/*.odc /home/tin/Desktop/data

Copy files based on size

Minimum Size

Similarly, you can also specify the minimum size that can be copied using rsync.

In this example, I am specifying the minimum size of 30K. Files with size less than 30K will not get copied using rsync.

rsync --min-size=30k /home/tin/Documents/*.odc /home/tin/Desktop/data

Define min. size in rsync

View progress

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

rsync --min-size=20k --progress /home/tin/Documents/*.odc /home/tin/Desktop/data

View copy progress in rsync

Copy an entire folder with rsync

Similar to files, you can also copy the entire folder and the files contained in it using rsync command.

Add below command in the terminal:

rsync --recursive /home/tin/Documents/ /home/tin/Desktop/documents

Copy an entire folder with rsync

Exclude files

You can also exclude certain files while copying the file and folder using rsync.

rsync -r --exclude="*.odc" /home/tin/Documents/ /home/tin/Desktop/documents

Exclude files from being copied with rysnc

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

That’s all was the basic usages of rsync utility. You can use rsync to copy and sync files between different directories in your Linux operating system.