15 useful rsync Commands in Linux

15 useful rsync commands in Linux

rsync command is used to sync (copy) files and directories locally and remotely. The important feature of rsync command is that it works on “delta transfer algorithm”, which means it will only sync or copy the changes from source to destination instead of copying the whole file which ultimately reduce amount of data sent over network.

15 useful rsync commands in Linux

Linux geeks generally use rsync command to manage day to day backup, mirroring, and restoration activities. It uses remote shell like SSH while synchronizing the files from local machine to remote machine and any user in the system can use rsync command as it does not require root or sudo privileges..

Here, we will discuss some useful rsync command examples in Linux, these examples will help specially Linux beginners to manage their sync, mirroring, and backup task more efficiently.

Rsync command is available for all UNIX and Linux operating systems. When we do minimal installation of CentOS & RHEL OS then rsync package is not part of default installation, so to install rsync on CentOS & RHEL, run the under yum command

# yum install rsync -y

In Debian operating Systems (Ubuntu & Linux Mint) use below apt command to install rysnc tool.

# apt install rsync -y

Syntax of rsync command:

Local Sync:

# rsync {options} {Source} {Destination}

Remote Sync pull:

 # rsync {options} <User_Name>@<Remote-Host>:<Source-File-Dir> <Destination>

Remote Sync Push:

 # rsync <Options> <Source-Files-Dir> <User_Name>@<Remote-Host>:<Destination>

Some of the commonly used options in rsync command are listed below:

-v–verboseVerbose output
-q–quietsuppress message output
-a–archivearchive files and directory while synchronizing
-r–recursivesync files and directories recursively
-b–backuptake the backup during synchronization
-u–updatedon’t copy the files from source to destination if destination files are newer
-l–linkscopy symlinks as symlinks during the sync
-n–dry-runperform a trial run without synchronization
-e–rsh=COMMANDmention the remote shell to use in rsync
-z–compresscompress file data during the transfer
-h–human-readabledisplay the output numbers in a human-readable format
-progressshow the sync progress during transfer

Let’s jump into the useful examples of rsync command

1.Copy or Sync files locally (rsync -zvh)

Let’s assume we want to copy a file from webhostc user’s home directory to /opt/back folder, execute the below rsync command

[root@webhostc ~]# rsync -zvh /home/webhostc/OpenStack-Networking.pdf /opt/backup
OpenStack-Networking.pdf
sent 4.09M bytes  received 35 bytes  2.73M bytes/sec
total size is 6.15M  speedup is 1.50
[root@webhostc ~]#

In above we have used the options like -z for compression, -v for verbose output and -h for human readable output.

2.Copy or Sync directory locally (rsync -zavh)

Let’s assume we want to copy or sync webhostc user’s home directory to /opt/backup folder, execute  the below rsync command with options -zavh,

[root@webhostc ~]# rsync -zavh /home/webhostc /opt/backup
sending incremental file list
webhostc/
webhostc/.bash_logout
webhostc/.bash_profile
webhostc/.bashrc
webhostc/OpenStack-Networking.pdf

sent 4.09M bytes  received 96 bytes  8.19M bytes/sec
total size is 6.15M  speedup is 1.50
[root@webhostc ~]#

Let’s verify whether directory is copied or not, execute below command,

[root@webhostc ~]# ls -ld /opt/backup/*
-rwx------. 1 root   root   6153239 Apr  7 00:25 /opt/backup/OpenStack-Networking.pdf
drwx------. 2 webhostc webhostc      90 Apr  7 00:25 /opt/backup/webhostc
[root@webhostc ~]#

As we can see “webhostc” directory is created on destination folder and its contents are also copied. In the above example if use trail / after source folder (/home/webhostc/) then rsync command will not create webhostc directory on destination but it will copy the contents only.

[root@webhostc ~]# rsync -zavh /home/webhostc/ /opt/backup

3.Copy files & directories recursively locally (rsync -zrvh or rsync -zavh)

Let’s assume we have multiple files and directories inside webhostc user home directory, use below rsync command to copy files and directories recursively, either use -a or -r option to copy files and directories recursively.

Note : In rsync command -a option is used for archiving during the copy or sync and apart from archiving -a option is also used for followings:

  • recursively copy files and directory
  • copy symlinks as symlinks
  • preserve permissions
  • preserve group
  • preserve modification time
  • preserve ownership
[root@webhostc ~]# rsync -zrvh /home/webhostc /opt/backup
or
[root@webhostc ~]# rsync -zavh /home/webhostc /opt/backup

4.Copy or sync files and directories from local to remote system

Let’s suppose we want to copy the folder “/home/webhostc/techi” from local machine to remote machine (192.168.1.2) under the /opt

[root@gateway ~]# rsync -zarvh /home/webhostc/tech root@192.168.1.2:/opt
root@192.168.1.2's password:
…………………………………………………………
tech/OTRS/
tech/OTRS/Database-Selection-OTRS-Installation-CentOS7.jpg
tech/OTRS/Install-OTRS-Accept-License.png
tech/OTRS/Install-OTRS-CentOS7-RHEL7.jpg
tech/OTRS/mysql-secure-installation-centos7-part2.jpg

sent 34.85M bytes  received 2.65K bytes  6.34M bytes/sec
total size is 37.77M  speedup is 1.08
……………………………………………………………
[root@webhostc ~]#

5.Copy or Sync files and directories from remote machine to local system

Let’s suppose we want to copy files and directories from remote machine(192.168.1.2) to our local system, in the below example I am copying remote folder “/opt/rpms_db” in my local machine under /tmp folder

[root@webhostc ~]# rsync -zarvh root@192.168.1.2:/opt/rpms_db /tmp
root@192.168.1.2's password:
receiving incremental file list
……………………………………………………………
rpms_db/
rpms_db/httpd-2.4.6-88.el7.centos.x86_64.rpm
rpms_db/httpd-tools-2.4.6-88.el7.centos.x86_64.rpm
rpms_db/postfix-2.10.1-7.el7.x86_64.rpm
rpms_db/pytalloc-2.1.13-1.el7.x86_64.rpm
rpms_db/samba-4.8.3-4.el7.x86_64.rpm
rpms_db/samba-client-libs-4.8.3-4.el7.x86_64.rpm
rpms_db/samba-common-4.8.3-4.el7.noarch.rpm
rpms_db/samba-common-libs-4.8.3-4.el7.x86_64.rpm
rpms_db/samba-common-tools-4.8.3-4.el7.x86_64.rpm
rpms_db/samba-libs-4.8.3-4.el7.x86_64.rpm

sent 484 bytes  received 15.45M bytes  1.07M bytes/sec
total size is 16.37M  speedup is 1.06
[root@webhostc ~]#

Above command will automatically create a folder “rpms_db” under the /tmp folder in our local machine.

[root@webhostc ~]# ls -l /tmp/
total 8
-rwx------. 1 root root  827 Apr  6 12:54 ks-script-avmuxw
drwxr-xr-x. 2 root root 4096 Apr  7 01:42 rpms_db
-rw-------. 1 root root    0 Apr  6 12:35 yum.log
[root@webhostc ~]# ls -l /tmp/rpms_db/
total 16028
……………………………………
-rw-r--r--. 1 root root 2844028 Nov 12 09:28 httpd-2.4.6-88.el7.centos.x86_64.rpm
-rw-r--r--. 1 root root   92504 Nov 12 09:28 httpd-tools-2.4.6-88.el7.centos.x86_64.rpm
-rw-r--r--. 1 root root 2558136 Nov 12 09:44 postfix-2.10.1-7.el7.x86_64.rpm
-rw-r--r--. 1 root root   17404 Nov 12 09:44 pytalloc-2.1.13-1.el7.x86_64.rpm
-rw-r--r--. 1 root root  696472 Nov 12 09:46 samba-4.8.3-4.el7.x86_64.rpm
-rw-r--r--. 1 root root 5070800 Nov 12 09:46 samba-client-libs-4.8.3-4.el7.x86_64.rpm
-rw-r--r--. 1 root root  210984 Nov 12 10:19 samba-common-4.8.3-4.el7.noarch.rpm
-rw-r--r--. 1 root root  167512 Nov 12 09:46 samba-common-libs-4.8.3-4.el7.x86_64.rpm
-rw-r--r--. 1 root root  458380 Nov 12 09:46 samba-common-tools-4.8.3-4.el7.x86_64.rpm
-rw-r--r--. 1 root root  282332 Nov 12 09:46 samba-libs-4.8.3-4.el7.x86_64.rpm
……………………………………
[root@webhostc ~]#

6.Specify remote shell during synchronization (rsync -e ssh)

In rsync command we can specify the remote shell like ssh during synchronization, let’s assume we want to use secure communication between source to destination machine via rsync then we should use ssh as remote shell, example is shown below

[root@webhostc ~]# rsync -zarvh -e ssh  root@192.168.1.2:/opt/rpms_db  /tmp
root@192.168.1.2's password:
receiving incremental file list
………………………………………………………………………………
rpms_db/
rpms_db/httpd-2.4.6-88.el7.centos.x86_64.rpm
rpms_db/httpd-tools-2.4.6-88.el7.centos.x86_64.rpm
rpms_db/postfix-2.10.1-7.el7.x86_64.rpm
rpms_db/pytalloc-2.1.13-1.el7.x86_64.rpm
rpms_db/samba-4.8.3-4.el7.x86_64.rpm
rpms_db/samba-client-libs-4.8.3-4.el7.x86_64.rpm
rpms_db/samba-common-4.8.3-4.el7.noarch.rpm
rpms_db/samba-common-libs-4.8.3-4.el7.x86_64.rpm
rpms_db/samba-common-tools-4.8.3-4.el7.x86_64.rpm
rpms_db/samba-libs-4.8.3-4.el7.x86_64.rpm

sent 484 bytes  received 15.45M bytes  1.82M bytes/sec
total size is 16.37M  speedup is 1.06
[root@webhostc ~]#

7.Display Synchronization progress in rsync command output

If you want to see the sync or copy progress in rsync command then use “–progress“, example is shown below

[root@webhostc ~]# rsync -avh --progress root@192.168.1.2:/opt/rpms_db /tmp
root@192.168.1.2's password:
receiving incremental file list
……………………………………………………………………………………………………..
rpms_db/
rpms_db/httpd-2.4.6-88.el7.centos.x86_64.rpm
          2.84M 100%   35.22MB/s    0:00:00 (xfr#6, to-chk=18/25)
rpms_db/httpd-tools-2.4.6-88.el7.centos.x86_64.rpm
         92.50K 100%    1.13MB/s    0:00:00 (xfr#7, to-chk=17/25)
rpms_db/postfix-2.10.1-7.el7.x86_64.rpm
          2.56M 100%   14.44MB/s    0:00:00 (xfr#17, to-chk=7/25)
rpms_db/samba-4.8.3-4.el7.x86_64.rpm
        696.47K 100%    3.71MB/s    0:00:00 (xfr#19, to-chk=5/25)
rpms_db/samba-client-libs-4.8.3-4.el7.x86_64.rpm
          5.07M 100%   19.90MB/s    0:00:00 (xfr#20, to-chk=4/25)
rpms_db/samba-common-4.8.3-4.el7.noarch.rpm
        210.98K 100%  844.42kB/s    0:00:00 (xfr#21, to-chk=3/25)
rpms_db/samba-common-libs-4.8.3-4.el7.x86_64.rpm
        167.51K 100%  667.70kB/s    0:00:00 (xfr#22, to-chk=2/25)
rpms_db/samba-common-tools-4.8.3-4.el7.x86_64.rpm
        458.38K 100%    1.77MB/s    0:00:00 (xfr#23, to-chk=1/25)
rpms_db/samba-libs-4.8.3-4.el7.x86_64.rpm
        282.33K 100%    1.09MB/s    0:00:00 (xfr#24, to-chk=0/25)

sent 484 bytes  received 16.38M bytes  3.64M bytes/sec
total size is 16.37M  speedup is 1.00
[root@gateway ~]#

8.Copy the directory structure without copying files

There are some scenarios where we want to copy the directory structure skiping files only from local machine to remote or vice versa.

Let’s suppose we want to copy the directory structure of “/home/webhostc” from local machine to remote machine (192.168.1.2) under /opt folder.

root@webhostc ~]# rsync -av -f"+ */" -f"- *" /home/webhostc root@192.168.1.2:/opt/
root@192.168.1.2's password:
building file list ... done
………………………………………………
webhostc/

sent 43 bytes  received 19 bytes  17.71 bytes/sec
total size is 0  speedup is 0.00
[root@webhostc ~]#

9.Resume large file transfer after getting failed in scp

There are some scenarios in linux admin profile where we have started copying a larger file using scp command, but it got terminated in the middle and we can’t afford to start copying it again using scp because of its large size and time consumption.

So in this situation rsync command can used as it  can start copying the file from where it left off or terminated, or in other words rsync can transfer the files which are partially copied using scp command. Example is shown below,

[root@webhostc ~]# scp root@192.168.1.2:/root/ubuntu-18.04-desktop-amd64.iso /opt
root@192.168.1.2's password:
ubuntu-18.04-desktop-amd64.iso                   28%  526MB  61.5MB/s   00:21 ETA
^CKilled by signal 2.
[root@gateway ~]#
[root@gateway ~]# rsync -P --rsh=ssh root@192.168.1.2:/root/ubuntu-18.04-desktop-amd64.iso /opt
root@192.168.1.29's password:
ubuntu-18.04-desktop-amd64.iso
  1,921,843,200 100%   18.47MB/s    0:01:39 (xfr#1, to-chk=0/1)
[root@webhostc ~]#

10.Delete files at destination if it is not present in source (–delete)

If you have already synced files from source to destination and from source you have deleted the files then you can force rsync command to delete the files on destination using the –delete option, example is shown below

[root@webhostc ~]# rsync -avz --delete /opt/rpms_db root@192.168.1.2:/tmp/rpms_db
root@192.168.1.2's password:
sending incremental file list
deleting rpms_db/apr-util-1.5.2-6.el7.x86_64.rpm
deleting rpms_db/apr-1.4.8-3.el7_4.1.x86_64.rpm
rpms_db/

sent 862 bytes  received 105 bytes  276.29 bytes/sec
total size is 15,947,152  speedup is 16,491.37
[root@webhostc ~]#

11.Put limit on file transfer size (–max-size)

If you don’t want to transfer or copy the large files using rsync then use the option ‘–max-size={specify-size-here}’, let’s assume we don’t we don’t want to transfer the files whose size is more than 500K,

Note: To specify the size in MB use M and for GB use G.

[root@webhostc ~]# rsync -avz --max-size='500K' /opt/rpms_db root@192.168.1.2:/tmp

12.Do not sync/copy the modified file at destination

There are some scenarios where we modified the files at destination and we don’t want to overwrite the modified files from source while syncing, so this can be easily achieved using -u option in rsync command

[root@webhostc ~]# rsync -avzu root@192.168.1.2:/opt/techi /home/webhostc/tech
root@192.168.1.2's password:
receiving incremental file list

sent 37 bytes  received 4,960 bytes  1,427.71 bytes/sec
total size is 37,771,649  speedup is 7,558.87
[root@webhostc ~]#

13.Remove files from source after synchronization (–remove-source-files)

Let’s suppose you want to delete files from source once the synchronization is completed using rsync command. In the below example, folder from local system “/home/webhostc/techi.tgz” is synced to remote system (192.168.1.3), once the synchronization is completed, it will delete the file from source.

[root@webhostc ~]# rsync --remove-source-files -a -v --progress --timeout=0 -e "ssh -p 22" root@192.168.1.3:/backup/backup.tar.gz /remotebackup/foldername/
root@192.168.1.3's password:
backup.tar.gz
sent 597 bytes  received 43 bytes  182.86 bytes/sec
total size is 518  speedup is 0.81
[root@webhostc ~]#

14.Dry run rsync command before actual synchronization

There can be some situations where we are not about behavior of rsync command so in such cases it is better to do dry run of rsync

[root@webhostc ~]# rsync --dry-run --remove-source-files -zvh /home/webhostc/backup.tar.gz  root@192.168.1.29:/opt
root@192.168.1.2's password:
projects.tar

sent 51 bytes  received 19 bytes  7.37 bytes/sec
total size is 981.24M  speedup is 14,017,682.29 (DRY RUN)
[root@webhostc ~]#

15.Include and Exclude files during synchronization with rsync

There are some situations where we want to copy or sync files & directories of specific type and want to exclude files or directories of specific type. Rsync command supports both include and exclude options.

In the below example want to copy the files of type pdf and rpm and exclude png file types.

[root@webhostc ~]# rsync -avz -e ssh --include '*.pdf *.rpm' --exclude '*.png' /home/webhostc/tech root@192.168.1.2:/opt
root@192.168.1.2's password:
sending incremental file list
tech/
tech/OpenStack-Networking.pdf
tech/httpd-2.4.6-88.el7.centos.x86_64.rpm
tech/httpd-tools-2.4.6-88.el7.centos.x86_64.rpm
tech/postfix-2.10.1-7.el7.x86_64.rpm

sent 9,469,912 bytes  received 96 bytes  2,705,716.57 bytes/sec
total size is 11,647,907  speedup is 1.23
[root@webhostc ~]#

16.Put restriction on data transfer speed in rsync (–bwlimit=<KB/s>)

In rsync command we can set the bandwidth limit for data transfer from one machine to another, use ‘–bwlimit=<KB/S>‘ option in rsync command to put limit on data transfer speed.

Let’s assume we want to set maximum data transfer rate (speed) is 600 KB/s with rsync, example is shown below

[root@webhostc ~]# rsync -avz --progress --bwlimit=600 /home/webhostc/tech root@192.168.1.2:/opt
root@192.168.1.2's password:
sending incremental file list
tech/
tech/OpenStack-Networking.pdf 6,153,239 100%  910.02kB/s    0:00:06 (xfr#1, to-chk=6/8)
tech/httpd-2.4.6-88.el7.centos.x86_64.rpm2,844,028 100%  615.28kB/s    0:00:04 (xfr#2, to-chk=5/8)
tech/httpd-tools-2.4.6-88.el7.centos.x86_64.rpm 92,504 100%  507.51kB/s    0:00:00 (xfr#3, to-chk=4/8)
tech/webhostc-2.png 0 100%    0.00kB/s    0:00:00 (xfr#4, to-chk=3/8)
tech/webhostc-3.png0 100%    0.00kB/s    0:00:00 (xfr#5, to-chk=2/8)
tech/webhostc.png0 100%    0.00kB/s    0:00:00 (xfr#6, to-chk=1/8)
tech/postfix-2.10.1-7.el7.x86_64.rpm2,558,136 100%  594.80kB/s    0:00:04 (xfr#7, to-chk=0/8)

sent 9,470,087 bytes  received 153 bytes  485,653.33 bytes/sec
total size is 11,647,907  speedup is 1.23
[root@webhostc ~]#

17.View the difference in files & directories between source and destination

Use “-i” option in rsync command to list the difference in files and directories between source and destination. Example is shown below

[root@webhostc ~]# rsync -avzi /home/webhostc/tech root@192.168.1.2:/opt
root@192.168.1.2's password:
sending incremental file list
.d..t...... tech/
<f.st...... tech/acctlist.txt

sent 438 bytes  received 45 bytes  138.00 bytes/sec
total size is 11,648,064  speedup is 24,116.07
[root@webhostc ~]#

As per above command output, there is difference in file called “acctlist.txt” on destination. Following are the meaning of the keywords in above output,

  • d: indicates change in destination file
  • f: indicates a file
  • t: indicates change in timestamps
  • s: indicates change in size

That’s all from this blog, in case these examples you to understand rsync command more efficiently then please do share your feedback and comments

To know about ‘find’ command for linux, Click Here

 

 

You may also like...

5 Responses

  1. Carol Xavier says:

    Most wanted rsync commands, I have always checked for a details blog and finally found one… Thanks admin

  2. Raj Sanjai says:

    Hello,

    This blog is much informative and useful.

    Thanks for the detailed post.

    Appreciate your effort, good luck

  3. Sinister says:

    For a beginner like me, This helped a lot…

  4. Kannan says:

    Thank you, great post.
    Rsync with deletion option in source server really helpful for me for maintaining the backups.

Leave a Reply

Your email address will not be published.