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
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
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.
rsync -e ssh /var/www/ email@example.com:/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
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
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
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
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
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.