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.
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.
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
This section will briefly review the rsync syntax.
rsync {option} {source} {destination}
rsync {option} {source} {user@host:destination}
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
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.
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.
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.
To view progress, use:
rsync –rv –progress {source} {destination}
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.
rsync can be used to list the contents of a source.
rsync {filename}
The above command lists the contents of the Documents directory.
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.
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.
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
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.
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.
2024 was the GenAI year. With new and more performant LLMs and a higher number…
Canonical’s Kubernetes LTS (Long Term Support) will support FedRAMP compliance and receive at least 12…
Welcome to the Ubuntu Weekly Newsletter, Issue 878 for the week of February 2 –…
At Canonical, we firmly believe that delivering an outstanding, customer-centric support experience is impossible without…
I want to share how to install osTicket v1.14 for Ubuntu 20.04 server. osTicket written…
Now I want to share how to install WordPress on ubuntu 20.04 server. WordPress is…