11 rsync command examples for Linux Beginners

rsync-command-examples

Rsync is a file coping tool that can copy files between a local system and remote system securely and efficiently. Rsync is also widely used for backups and mirroring. Rsync only needs to copy the differences between the systems that mean, let say we have 2 directory names as directory1 and directory2. Now in directory1 we have 2 files named as file1 and file2, in directory2 we have only one file i.e file1 so when we run rsync command to copy files from directory1 to directory2 then it will only copy file2 as file1 already exists.

As stated earlier rsync can be used to copy files locally or remotely. Let’s see the syntax to run them.

Syntax to run rsync locally is:

# rsync options {source i.e from where we want to copy} {destination i.e place where we want to copy}

eg # rsync –av /directory1 directory2

Note: If destination directory is not created then it will be created automatically when we run rsync. Like here let say if we have not created directory2 but while running command we have mentioned destination as directory2 then it will be created.

Syntax to run rsync remotely:

# rsync options {source} {user@host:/path}

example is shown below,

# rsync –av /directory1 student@server1.example.com:/directory2

There are many advantages of rsync, let see few of them:

  • Rsync copy files securely from one system to another.
  • It is very versatile file coping tool, it has many options that can be used while coping.
  • It do not need to have super-user privileges in order to use it.
  • It is much faster way to copy files.
  • If while coping files over the network, if network gets disconnected by any reason the coping start from where it was stopped due to network issue not like scp that it will start coping from beginning.
  • In rsync we have option to compress and then copy the files hence increasing the speed and it takes less time to copy.

Now let see few example of rsync:

Before we proceed here in most of the examples we have taken 2 directories that are directory1 and directory2. In directory1 we have 3 files named as file1, file2 and file3 while directory2 is empty.

Example:1 Copy/Transfer data in a local machine

we can use rsync command to copy/transfer data in our local machine from one destination to other.

[root@desktop ~]# 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 run

Lets assume we are beginner and we don’t know that what will happen if we run the rsync command. In that case we need not to worry there is an option in rsync which we can use to check what happens when the command really gets executed. The option used is “-n”. After checking the output if we get desire result then we can run the command without option “-n”.

[root@desktop ~]# 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 verbosity to the output as coping proceed.
  • -n is used for dry run.

Example:3 Archiving mode in rsync command via option -a

rsync do not preserve permission and timestamps of the file while coping.In order to that we need to use option –a.

It allows copying of files recursively in archiving mode and it also preserves symbolic links, file permissions, user & group ownerships and timestamps.

[root@desktop ~]# 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 Compressing files during copy/transfer in rsync

Let assume if files that we need to copy are too large then we can compress the file by using option –z .

[root@desktop ~]# 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 Coping/Transfer data from local machine to a remote machine

In above example which we have seen in that we have copied files locally, now let’s try to copy files to remote system using rsync command

[root@desktop ~]# rsync -avze ssh /var/log root@192.168.122.1:/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 Coping/Transfer data along with specific protocol (option -e)

If we want to use rsync command with any specific protocol then we need to use option -e.

Like here we have used “ssh” where we can take remote session of machine by entering the user password. Then we would like to proceed with coping of files.

[root@desktop ~]# rsync -avze ssh /var/log root@192.168.122.1:/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.
root@192.168.122.1'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 option (–include , –exclude)

Let’s assume that we want to copy only txt files and we want to exclude all other files, then in this situation we can use –include/ –exclude option.

[root@desktop ~]# rsync -azv --include '*.txt' --exclude '*' directory1/ directory2/

Example:8 Set Max file size during copy or transfer in rsync command (–max-size)

To set the max size of files that need to be transferred, there is an option in rsync where we can define the maximum size of file that we want to transfer and the option for it is “–max-size”, (–max-size=”mention the size of file”), example is shown below,

[root@desktop ~]# rsync -avz –max-size=500k /var/log /tmp

Example:9 View the progress of data transfer in rsync command (–progress )

If you want to see how much time is left for complete transfer of files then we can use “–progress” option.

[root@desktop ~]# rsync -avz --progress /var/log /tmp

Example:10 Set bandwidth limit during copy/transfer File (–bwlimit={set bandwidth limit})

There is an option in rsync where you can even set bandwidth limit while transferring file.

[root@desktop ~]# rsync -avze --bwlimit=200 /var/log /tmp

Example:11 Automatically deleting the source files after successful copy/Transfer

There situations where we don’t want to keep the files/data in source after transferring it to destination. In that case we can use option “–remove-source-files”.

[root@desktop ~]# rsync -azv --remove-source-files directory1/ directory2/

Above mentioned are few examples of rsync commands. There are many more options. Try and play with this command its very useful command for syncing/coping/transferring the data.

Leave a Comment

6 + 3 =