rsync command also known as the remote sync is used for transferring and synching files and directories between machines, hosts, remote shells, and so on. It is a powerful utility that you should know about if you work with Linux.
Here at LinuxAPT, we shall look into how to install the rsync command on your Ubuntu Linux system which can also be applied in other Linux distributions.
If you want to sync your files using scp command, visit: https://linuxapt.com/blog/408-top-15-scp-command-examples-in-linux
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.
To see more of the available options on rsync, use:
$ rsync –help
In order to install rsync, you need to run the following command on your system:
$ sudo apt-get install rsync
The rsync syntax is given below.
1. Local to local transfer or sync:
$ rsync {option} {source} {destination}
2. Local to remote transfer or sync:
$ rsync {option} {source} {user@host:destination}
3. Remote to local transfer or sync:
$ rsync {option} {user@host:destination} {destination}
Here,
1. Copy files locally
To copy files locally, use:
$ rsync –a {filename} {destination}
Here, 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
You will see that the "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 our case, we will copy "source.zip" again, but this time, rename it to "destination.zip".
"destination.zip" has been copied to the Documents directory.
2. 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}
Now you will see that the content "Source" directory being copied to the "Destination" directory.
3. 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}
Now, you will see that the "Source" directory and its contents are copied to the "Destination" directory.
4. Show progress
To view progress, use:
$ rsync –rv –progress {source} {destination}
5. Ignore files
If you want to ignore certain files, use:
$ rsync -vr --exclude '{condition}' {source} {destination}
The "Source" directory will have some files when you run the "ls" command.
You can ignore files that start with an "s".
6. List file and directories
rsync can be used to list the contents of a source:
$ rsync {filename}
This command will list the contents of the Documents directory.
7. 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.
8. Copy files from a remote to a local machine
You can use the below command like the one used in the above example:
$ rsync –a {remote source} {local username@local IP:/destination}
9. 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)
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}/
Here,
The parameter "f" means file. Other parameters include:
10. Limit bandwidth when transferring files
To limit the bandwidth while transferring files, use:
$ rsync -vr --bwlimit=1000 {source}/* {destination}
To uninstall only the rsync package we can use the following command:
$ sudo apt-get remove rsync
We can use the following command to remove rsync configurations, data and all of its dependencies, we can use the following command:
$ sudo apt-get -y autoremove --purge rsync
This article covers the basics of rsync command, how to transfer and sync files locally as well as remotely. In fact, Rsync is the most efficient, accessible, and secure ways to sync files between operating systems and servers.
How to Install Rsync On Linux System ?
1. Install Rsync Alma Linux, Fedora, Rhel, Rocky Linux based distributions:
$ sudo yum install rsync -y
2. Install Rsync Debian, Linux Mint, Ubuntu based distributions:
$ sudo apt install rsync -y
Rysnc Commands includes: