In Linux, the rsync “Remote Synchronization” command is used to copy files from one location to another, whether local or remote. The users can efficiently transfer and synchronize the files between the systems, which is quite handy when transferring data to or from a server. It also supports remote file transferring over SSH.
This guide sheds light on using rsync to copy files on Linux:
- Installing rsync on Linux
- What is the Syntax of Using rsync Command?
- How to Copy Files Using the rsync Command?
Installing rsync on Linux?
The rsync comes pre-installed, but you can check if rsync is installed or not by running the following command:
$ which rsync
If you see anything other (error) than the output above, you need to install it using either of the following commands depending on your distro.
$ sudo yum install rsync #For CentOS/Fedora $ sudo apt install rsync #For Ubuntu $ sudo pacman -S rsync #For Arch Linux $ sudo zypper install rsync #For OpenSUSE
What is the Syntax of Using rsync Command?
The rsync command of Linux is used in the following syntax.
$ rsync options source destination
For the options, hop on to the next heading.
The rsync command of Linux comes with many options/flags used to customize the utility’s behavior. Here are some most commonly used flags among them.
- -a, –archive: This option stands for “archive” and preserves the file permissions, ownership, and timestamps.
- -v, –verbose: This option stands for “verbose” and is used to view the progress of the transfer(s).
- -z, –compress: This option is used to compress the data.
- -h, –human-readable: This option displays the transfer sizes in a human-readable format, such as “K” for kilobytes, “M” for megabytes, and “G” for gigabytes.
- –max-size: Specify the maximum size
- –min-size: Specify the minimum size
To get more help with the flags, use this command.
$ rsync --help
Note: All the options/flags of the rsync command can be used with each other as an argument.
How to Copy Files Using the rsync Command?
One of the best ways to copy files (locally) using a terminal is through the rsync command in this format.
$ rsync [option] [source] [destination]
The options are discussed above, and the source is the path from where the file is to be copied with the name of the files/directories, while the destination is where the file would be copied to.
Example 1: Copy Files From One Directory to Another Directory
Here’s an example of copying the file from the ‘Music’ directory to the directory named ‘copied’ inside the ‘tmp’ directory.
$ rsync -avzh Music/file1 /tmp/copied
The explanation of the above command is mentioned below:
The “a” stands for archive mode, the “v” for verbose, the “z” for compress, and the “h” for human readable. While the “Music/file1” and “/tmp/copied” represents the source and destination directories.
It can be seen in the above image that the original size was 130.30 MB but was reduced to 37.23 MB when received by the destination directory.
Example 2: Copying Files by Specifying the Maximum File Size
You can also specify the maximum file sizes to be copied using the following command:
$ rsync -a --max-size=400k Music/ /tmp/copied/
In the above command, “–max-size=400k” indicates that all the files will be copied from the Music directory where the size is less than 400 KB.
Example 3: Copying Files by Specifying the Minimum File Size
The same thing could be applied by specifying the minimum file size as shown below
$ rsync -a –-min-size=100m Music/ /tmp/copied/
In the above command, “–min-size=100m” when executed, copies all the files whose size is more than 100MB from the source (Music) to the destination (/tmp/copied).
Furthermore, if you are interested in transferring files remotely, then you can read this article for more details.
The rsync is a command line tool that is used to copy and synchronize files efficiently. The key advantage of rsync is that it starts transferring files from where it left off in case of interruptions. You can also compress files to reduce the size and can also apply the size limit for file transferring. This guide shows how users can use the rsync command to copy the files or directories.