How to use rsync

How to Use Rsync

Introduction

The remote sync, or rsync command is used for transferring and synching files and directories between machines, hosts, remote shells, or a combination of the above. It is a powerful utility that you should know about if you work with Linux. We will cover the basics of the rsync command here. Though this article uses Ubuntu 20.04, you can use the same commands on other Linux distributions.

What makes rsync so special?

The rsync commands uses compression and decompression when receiving and sending files, this means it will consume very less bandwidth. Rsync only updates the differences between the source and destination as it uses the remote update protocol. It also supports owners, groups, links, permissions, and devices.

How
Sponsored
to install rsync?

Most Linux distributions come with rsync installed. In case yours doesn’t, use the following command on your Ubuntu system:

How to use rsync 1

sudo apt-get install rsync

How to use rsync 2

Rsync syntax

This section will briefly review the rsync syntax.

Local to local transfer or sync

rsync {option} {source} {destination}

Local to remote transfer or sync

rsync {option} {source} {user@host:destination}

Remote to local transfer or sync

rsync {option} {user@host:destination} {destination}

Source is the source directory

Destination is the destination directory

Option is the rsync command options

User is the remote username

Host is the IP or remote hostname

How to use rsync

Copy files locally

To copy files locally, use:

rsync –a {filename} {destination}

How to use rsync 3

In the above example, we are copying “source.zip” from our current directory to Documents.

You can check if the file was copied by listing all the files and directories in Documents, using:

ls

How to use rsync 4

As you can see, “source.zip” is present in the Documents directory.

If you don’t add a name for the file in the destination, it will copy the same name as the source file. To change the name of the destination file, use:

rsync –a {filename} {destination/filename}

In the example below, we will copy “source.zip” again, but this time, rename it to “destination.zip”.

How to use rsync 5

destination.zip” has been copied to the Documents directory, as shown in the image below.

How to use rsync 6

Copy the contents of a directory to the destination

You can use the command below to copy the contents of your source directory to the destination.

rsync –av {source}/ {destination}

How to use rsync 7

The above image shows the content “Source” directory being copied to the “Destination” directory.

Copy the whole directory to the destination

If you only want to copy the contents of your directory to the destination, use:

rsync –av {source} {destination}

How to use rsync 8

The above example copies the “Source” directory and its contents to the “Destination” directory.

Show progress

To view progress, use:

rsync –rv –progress {source} {destination}

How to use rsync 9

Ignore files

If you want to ignore certain files, use:

rsync -vr --exclude '{condition}' {source} {destination}

The “Source” directory has the following files:

How to use rsync 10

In the example below, we will ignore files that start with an “s”.

How to use rsync 11

Sponsored

As the image shows, only “file” is copied.

List file and directories

rsync can be used to list the contents of a source.

rsync {filename}

How to use rsync 12

The above command lists the contents of the Documents directory.

Copy files from a local to a remote machine

For moving files to a remote machine, you will need to install openssh-server. The command for that is:

sudo apt update
sudo apt install openssh-server

How to use rsync 13

How to use rsync 14

Once this has been installed, you can use the command below to move your file or directory.

rsync –a {source} {remote username@remote IP:/destination}

You will be asked for the password of the remote machine.

In the example below, we will move “doc” to the remote home directory.

How to use rsync 15

Using ls, we can see that “doc” has been moved here.

How to use rsync 16

Copy files from a remote to a local machine

You will use the same command like the one used in the above example.

rsync –a {remote source} {local username@local IP:/destination}

In this example, we will be moving “remotesrcdoc” from the remote machine to the local machine.

See also  How to Turn Off Laptop Screen in Ubuntu 22.04

How to use rsync 17 Using ls, we can see that “remotesrcdoc” has been moved here.

How to use rsync 18

Add current date to the destination file

You can add the date to your destination file or directory’s name using:

rsync -av /home/test/Desktop/Linux /home/test/Desktop/rsync$(date +\%Y-\%m)

In the example below, we will move “source.zip” to the Desktop, name it destination, and add the date to it.

How to use rsync 19

How to use rsync 20

Viewing the differences between the source and destination files
The command below can be used to see the differences between the source and destination files.

rsync –avzi {source}/ {destination}/

How to use rsync 21

The image below shows that the files, “Donuts” and “Hello.txt” are not present in the destination directory.

The parameter “f” means file. Other parameters include:

t: change in timestamp

d: change in destination

s: change in file size

Limit bandwidth when transferring files

To limit the bandwidth while transferring files, use:

rsync -vr --bwlimit=1000 {source}/* {destination}

In the example below, we are limiting the bandwidth to 1000kB.

How to use rsync 22

More on rsync

To see more of the available options on rsync, use:

rsync –help

How to use rsync 23

We covered some of the basics of rsync command, we saw how to transfer and sync files locally and remotely.

If you want to sync your files using scp command, visit here.

Karim buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.

Leave a Comment

Only people in my network can comment.