Linux Rsync command (remote sync)

The Linux rsync command efficiently transfers and synchronizes files or directories between a local machine, a remote server, or any of these. Anyone who works with Linux based systems should use this powerful one to improve their productivity. With the help of this article, you will learn everything you need to know to start using the rsync command.

Manually syncing folders or copying files can take a long time. Linux’s rsync utility can do most of the work for you while adding great time-saving features. Even if you lose the connection during the transfer, this tool will continue exactly where it left off once the connection is restored.

Cloud VPS Hosting with the highest virtual server reliability and performance! True virtual servers built for speed.

basic syntax

The basic syntax for rsync works as follows:

rsync

There are a couple of different ways you can use rsync on Linux. In this example, indicates the actions to be carried out, is the source directory and is the destination directory or machine.

Basic Syntax for Remote Shell

When using a remote shell, such as SSH or RSH, the rsync syntax is slightly different.

To access the remote shell (PULL) use the rsync command:

rsync HOST:SRC

To access the remote shell (push) use the rsync command:

rsync SRC HOST:

How to check the version of Rsync

Before checking if Rsync is installed, we need to login to the VPS server that we will be using. This handy will show you how to do it on a Windows machine using SSH. If you’re using a MacOS or Linux computer, use the terminal instead.

Rsync comes pre-installed with many Linux distributions. To check if rsync is installed on your machine, run the following command:

rsync -version

On our Ubuntu distribution, the command produced the following output:

rsync version 3.1.2 protocol version 31

That means rsync version 3.1.2 is already on our machine. Easy, right?

Installing Rsync

If your machine doesn’t have rsync pre-installed, you can do it manually in just a minute! In distributions based on DebianWhat Ubuntuyou can do it using the following commands:

apt-get install rsync

In distributions like fedora Y CentOSuse the following command:

yum install rsync

And in macOS you can use the following command:

brew install rsync

That is all! Linux Rsync will be ready to sync data, perform file transfers and delete files. You can check if the installation was successful using the command mentioned above:

See also  DNS files

rsync -version

Working with Rsync

For this tutorial, we create two directories on our Linux desktop named Original Y Duplicate. The original directory contains three images while the duplicate is empty. Now let’s see how Rsync will create new ways to increase productivity.

To create two test directories, use the following commands:

cd ~ mkdir original mkdir duplicate touch original/file{1..3}

If you want to double check, use the command ls to list all files inside the directory:

ls original

The output will look similar to this:

file1 file2 file3

If you use the command ls with the duplicate directory, the result should be empty.

Now that the directories are set up, let’s try a few more commands.

The following command will copy or sync all files in the directory original In the address book duplicate.

original rsync/*duplicate/

* tells the rsync command to sync everything in the original directory with the duplicate.

If we put a new image or file in the original folder and run the same command again, only the new image will be copied to the destination.

This feature is useful when copying files over a network with limited bandwidth.

Most common Rsync commands

Here is a list of the most common commands used with rsync:

This enables archive mode.

-a, –archive

The next one gives you a visual output showing the progress of the process.

-v, –verbose

This outputs in a human readable format.

-h, –human-readable format

The following compresses the file data during transfer.

-z, –compress

This is for copying data recursively

-r

How to use Rsync commands with subdirectories

Note that this command will only copy files from the parent directory of the folder Originalbut none of the subdirectories.

If you also want to copy the subdirectories, you will need to use this command:

rsync -r original/ duplicate/

The option -r (-recursive) tells rsync to copy everything, including subdirectories and the files in our original folder.

the modifier / used after original tells rsync that copy the contents of the original directory in the duplicate folder.

How to sync files

If what you want is sync filesie to copy files that could be in the destination folder, but are not in the original folder, to the original folder, use the following command:

rsync -r original duplicate/

See also  What is FTP: FTP Explained for Beginners

With this command, we can be sure that both the original and the duplicate folder contain the same files.

How to combine Rsync commands

Another useful option is -a (-file), which can be joined with many other commands. That means it not only copies files, but also copies permissions, modification times, and any other dates.

Use the command -a with -v would look similar to this:

rsync -av –dry-run original/ duplicate/

Don’t worry, it’s not as complicated as it seems. Let’s break it down.

This command will only display the files to be copied but without making any actual changes. With this command, you can get a list of the files that will be copied.

If all the files shown are the ones you want to copy, run the command again but not including –dry-run.

–dry-run (either -n) causes rsync to perform a test run that does not make any changes.

Other Options for Rysnc Commands

By adding the option -a to option -vgetting -av in our command increases the verbosity. This is how it should look like:

rsync -av original/ duplicate/

If you want to sync the two folders and remove duplicate items that are not present in the original folder, add -deleteSo:

rsync -av –delete original/ duplicate/

You can too Exclude specific files or subdirectories when you sync two folders. You can do it by adding -exclude=. If you need to specify more than one file, separate them with a comma.

rsync -av –exclude=file1,file2 original/ duplicate/

You can too include specific files or subdirectories when you’re syncing. Do this by adding -include=. You can also use this with the option -exclude=. The following example will include files beginning with the letter L and will exclude all other files:

rsync -av –include=L* –exclude=* original/ duplicate/

With Rsync, you can also specify the size of the file to sync. To do this, use the option –max-size:

rsync -av –max-size=10k original/ duplicate/

Using the option -z (-compress) you will compress the files that are being transferred over the network. And as an added bonus, let’s see how to make a transfer from the origin server to another. The command would look like this:

rsync -za ~/Desktop/Original edward@192.168.22.90:~/tmp/

As mentioned earlier, -z compress files, -aor just adding “a” to -z, will ensure that all permissions are also copied.

~/Desktop/Original is the source. It’s a local directory: the one on the machine you’re logged into, and lastly, edward@192.168.22.90: ~/tmp/ indicates the destination. edward@192.168.22.90 is the destination remote server address, while: ~/tmp/ points to a specific folder on that machine.

See also  How to Inspect an Element in Chrome: Easy Ways to Edit a Web Page

How to add a progress bar

In addition to the above commands, you can add -P which will combine the options -progress Y -partial. This will give you a progress bar for the files being transferred and will also allow you to resume any interrupted file transfers.

rsync -azP

The result will look similar to this:

sending incremental file list ./ file1 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=1/3) file2 0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=2/3) file3 0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=3/3)

Then if you run the command again, you will see a shorter output. This is because no new changes have been made. The result will look similar to this:

sending incremental file list send 26 bytes received 5 bytes 1200.00 bytes/sec total size is 0 speedup is 0.00

If you only want to transfer certain files, you can specify them with the following command:

touch original/file{1..5} rsync -azP

This will give a result similar to the previous command, but only with the files specified in square brackets.

How to create an Rsync backup

Another important command is the one that allows you to create an Rsync backup. You can do it by combining –backup with the command –dir so you can specify where the backup files will be stored.

rsync -a –delete –backup –backup-dir=/path/to/backup /path/to/SRC

conclusion

We have only seen the tip of the iceberg. Linux Rsync is an incredibly powerful utility that every Linux server administrator or developer should know about. We’ve covered everything you need to get started, from installation to basic commands. For more advanced features, check out the .

Get ready to improve your productivity! And good luck with your project.

Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.

Loading Facebook Comments ...
Loading Disqus Comments ...