11 Examples of rsync commands for Linux beginners

Rsync is a file copy tool that allows you to copy files safely and efficiently between local and remote systems. Rsync is also widely used for backup and mirroring. Rsync only copies differences between systems. That is, suppose you have two directory names, directory1 and directory2. Now, directory1 has two files named file1 and file2, and directory2 has only file1.When I run the rsync command and copy the files from directory1 to directory2, only file2 already exists because file1 already exists Will be copied.

As mentioned earlier, rsync can be used to copy files locally or remotely. Let’s look at the syntax to execute them.

The syntax for running rsync locally is:

#Rsync options {source location, ie destination location} {destination location, destination location}

Example #rsync –av / directory1 directory2

Note: If the destination directory has not been created, it will be created automatically when you execute rsync. If directory2 is not created as shown here, but destination is specified as directory2 during command execution, it will be created.

Syntax for running rsync remotely:

#Rsync option {source} {[email protected]:/road}

An example is shown below.

# rsync –av /directory1 [email protected]:/directory2

rsync has many advantages, let’s look at some of them.

  • Rsync copies files securely between systems.
  • It is a very versatile file copy tool, with many options available during copying.
  • You do not need to have superuser privileges to use it.
  • Copying files is much faster.
  • If you are copying a file over the network and the network is disconnected for any reason, unlike scp, the copy starts where it stopped due to network problems and starts over from the beginning.
  • rsync has the option of compressing the file before copying it, which improves speed and reduces the time it takes to copy.

Let’s look at some examples of rsync.

Before proceeding with most examples, we got two directories, directory1 and directory2. directory1 has three files named file1, file2, and file3, but directory2 is empty.

Example 1: Copy / Transfer data on local machine

You can use the rsync command to copy / transfer data on your local machine from one destination to another.

[[email protected] ~]# rsync -av directory1/ directory2/
sending incremental file list
./
file1
file2
file3
sent 245 bytes received 72 bytes 634.00 bytes/sec
total size is 55 speedup is 0.17

Example: 2 rsync dry runs

Suppose we are a beginner and do not know what happens when we run the rsync command. In that case, don’t worry, there are rsync options you can use to see what happens when the command is actually executed. The option used is “-n”. After reviewing the output, if you get the desired result, you can run the command without the option “-n”.

[[email protected] ~]# rsync -vn directory1 directory2
skipping directory directory1
sent 8 bytes received 12 bytes 40.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)
  • -v is for output verbosity as the action progresses.
  • -n is used for dry run exercises.

Example: Archive mode of rsync command by 3 options -a

rsync does not preserve file permissions and timestamps during copying. To do so, you must use the option -a.

In addition to recursively copying files in archive mode, it preserves symbolic links, file permissions, user and group ownership, and timestamps.

[[email protected] ~]# rsync -av directory1/ directory2/
sending incremental file list
./
file1
file2
file3
sent 245 bytes received 72 bytes 634.00 bytes/sec
total size is 55 speedup is 0.17

Example: 4 Compress files during copy / transfer with rsync

If the file that needs to be copied is too large, you can use the option -z to compress the file.

[[email protected] ~]# rsync -azv directory1/ directory2/
sending incremental file list
./
file1
file2
file3
sent 233 bytes received 72 bytes 610.00 bytes/sec
total size is 55 speedup is 0.13

Example: 5 Copy / Transfer data from local machine to remote machine

In the example above, which verified that the file was copied locally, let’s use the rsync command to copy the file to the remote system.

[[email protected] ~]# rsync -avze ssh /var/log [email protected]:/tmp
sending incremental file list
log/
log/Xorg.0.log
log/Xorg.0.log.old
log/Xorg.1.log
log/Xorg.9.log
log/boot.log
log/btmp
sent 1116324 bytes received 1760 bytes 131539.29 bytes/sec
total size is 11511054 speedup is 10.30

Example: 6 Copy / Transfer data according to a specific protocol (option -e)

If you want to use the rsync command with a particular protocol, you must use the option -e.

As shown here, I used “ssh”, which allows me to enter a user password and get a remote session on the machine. Next, I want to proceed with copying the files.

[[email protected] ~]# rsync -avze ssh /var/log [email protected]:/tmp
The authenticity of host '192.168.122.1 (192.168.122.1)' can't be established.
ECDSA key fingerprint is 6c:43:1e:b7:8a:8d:82:d4:c2:7c:ac:43:fd:d4:69:86.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.122.1' (ECDSA) to the list of known hosts.
[email protected]'s password:
sending incremental file list
log/
log/Xorg.0.log
log/Xorg.0.log.old
log/Xorg.1.log
log/Xorg.9.log
log/boot.log
log/btmp
log/btmp-20180309
log/cron
log/cron-20171206
log/dmesg
log/dmesg.old
log/lastlog
log/maillog
log/maillog-20171206
sent 1116324 bytes received 1760 bytes 131539.29 bytes/sec
total size is 11511054 speedup is 10.30

Example: 7 rsync include and exclude options (–include, –exclude)

If you want to copy only the txt file and exclude all other files, you can use the -include / -exclude options in this situation.

[[email protected] ~]# rsync -azv --include '*.txt' --exclude '*' directory1/ directory2/

Example: Setting the maximum file size during copying or transferring with the 8 rsync command (–max-size)

To set the maximum size of files that need to be transferred, rsync has an option that allows you to define the maximum size of files to be transferred. The options are “-max-size”, (-max-size = “refer to the size of the file”), for example:

[[email protected] ~]# rsync -avz –max-size=500k /var/log /tmp

Example: 9 Displaying the progress of data transfer with the rsync command (–progress)

If you want to see how much time is left for a complete transfer of the file, you can use the “–progress” option.

[[email protected] ~]# rsync -avz --progress /var/log /tmp

Example: Set bandwidth limit while copying / transferring 10 files (–bwlimit = {set bandwidth limit})

rsync has an option that allows you to set a bandwidth limit while transferring files.

[[email protected] ~]# rsync -avze --bwlimit=200 /var/log /tmp

Example: Automatically delete source files after 11 successful copies / transfers

After transferring files / data to the destination, there are situations where you do not want to keep the files / data in the source. In that case, you can use the option “–remove-source-files”.

[[email protected] ~]# rsync -azv --remove-source-files directory1/ directory2/

The above are some examples of the rsync command. There are even more options. Try this command and use one that is very useful for data sync / copy / transfer.

.

Sidebar