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
to install rsync?
Most Linux distributions come with rsync installed. In case yours doesn’t, use the following command on your Ubuntu system:
sudo apt-get install rsync
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}
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
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”.
“destination.zip” has been copied to the Documents directory, as shown in the image below.
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}
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}
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}
Ignore files
If you want to ignore certain files, use:
rsync -vr --exclude '{condition}' {source} {destination}
The “Source” directory has the following files:
In the example below, we will ignore files that start with an “s”.
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}
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
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.
Using ls, we can see that “doc” has been moved here.
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.
Using ls, we can see that “remotesrcdoc” has been moved here.
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.
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}/
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.
More on rsync
To see more of the available options on rsync, use:
rsync –help
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 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.