As the name suggests, rsync command is used to sync (or copy) files and directories locally and remotely. One of the important feature of rsync is that it works on “delta transfer algorithm”, 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. 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.
In this article we will discuss 17 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 like 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 beneath yum command
~]# yum install rsync -y
In Debian Like 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, –verbose Verbose output
◈ -q, –quiet suppress message output
◈ -a, –archive archive files and directory while synchronizing ( -a equal to following options -rlptgoD)
◈ -r, –recursive sync files and directories recursively
◈ -b, –backup take the backup during synchronization
◈ -u, –update don’t copy the files from source to destination if destination files are newer
◈ -l, –links copy symlinks as symlinks during the sync
◈ -n, –dry-run perform a trial run without synchronization
◈ -e, –rsh=COMMAND mention the remote shell to use in rsync
◈ -z, –compress compress file data during the transfer
◈ -h, –human-readable display the output numbers in a human-readable format
◈ –progress show the sync progress during transfer
Let’s jump into the useful examples of rsync command
Example:1) Copy or Sync files locally (rsync -zvh)
Let’s assume we want to copy a file from pkumar user’s home directory to /opt/back folder, execute the below rsync command
[root@gateway ~]# rsync -zvh /home/pkumar/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@gateway ~]#
In above we have used the options like -z for compression, -v for verbose output and -h for human readable output.
Example:2) Copy or Sync directory locally (rsync -zavh)
Let’s assume we want to copy or sync pkumar user’s home directory to /opt/backup folder, execute the below rsync command with options -zavh,
[root@gateway ~]# rsync -zavh /home/pkumar /opt/backup
sending incremental file list
pkumar/
pkumar/.bash_logout
pkumar/.bash_profile
pkumar/.bashrc
pkumar/OpenStack-Networking.pdf
sent 4.09M bytes received 96 bytes 8.19M bytes/sec
total size is 6.15M speedup is 1.50
[root@gateway ~]#
Let’s verify whether directory is copied or not, execute below command,
[root@gateway ~]# ls -ld /opt/backup/*
-rwx------. 1 root root 6153239 Apr 7 00:25 /opt/backup/OpenStack-Networking.pdf
drwx------. 2 pkumar pkumar 90 Apr 7 00:25 /opt/backup/pkumar
[root@gateway ~]#
As we can see “pkumar” directory is created on destination folder and its contents are also copied. In the above example if use trail / after source folder (/home/pkumar/) then rsync command will not create pkumar directory on destination but it will copy the contents only.
[root@gateway ~]# rsync -zavh /home/pkumar/ /opt/backup
Example:3) Copy files & directories recursively locally (rsync -zrvh or rsync -zavh)
Let’s assume we have multiple files and directories inside pkumar 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@gateway ~]# rsync -zrvh /home/pkumar /opt/backup
or
[root@gateway ~]# rsync -zavh /home/pkumar /opt/backup
Example:4) Copy or sync files and directories from local to remote system
Let’s suppose we want to copy the folder “/home/pkumar/lpicentral” from local machine to remote machine (192.168.1.29) under the /opt
[root@gateway ~]# rsync -zarvh /home/pkumar/lpicentral root@192.168.1.29:/opt
root@192.168.1.29's password:
…………………………………………………………
lpicentral/OTRS/
lpicentral/OTRS/Database-Selection-OTRS-Installation-CentOS7.jpg
lpicentral/OTRS/Install-OTRS-Accept-License.png
lpicentral/OTRS/Install-OTRS-CentOS7-RHEL7.jpg
lpicentral/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@gateway ~]#
Example: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.29) to our local system, in the below example I am copying remote folder “/opt/rpms_db” in my local machine under /tmp folder
[root@gateway ~]# rsync -zarvh root@192.168.1.29:/opt/rpms_db /tmp
root@192.168.1.29'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@gateway ~]#
Above command will automatically create a folder “rpms_db” under the /tmp folder in our local machine.
[root@gateway ~]# 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@gateway ~]# 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@gateway ~]#
Example: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@gateway ~]# rsync -zarvh -e ssh root@192.168.1.29:/opt/rpms_db /tmp
root@192.168.1.29'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@gateway ~]#
Example: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@gateway ~]# rsync -avh --progress root@192.168.1.29:/opt/rpms_db /tmp
root@192.168.1.29'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 ~]#
Example: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/pkumar” from local machine to remote machine (192.168.1.29) under /opt folder.
root@gateway ~]# rsync -av -f"+ */" -f"- *" /home/pkumar root@192.168.1.29:/opt/
root@192.168.1.29's password:
building file list ... done
………………………………………………
pkumar/
sent 43 bytes received 19 bytes 17.71 bytes/sec
total size is 0 speedup is 0.00
[root@gateway ~]#
Example: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@gateway ~]# scp root@192.168.1.29:/root/ubuntu-18.04-desktop-amd64.iso /opt
root@192.168.1.29'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.29:/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@gateway ~]#
Example: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@app ~]# rsync -avz --delete /opt/rpms_db root@192.168.1.28:/tmp/rpms_db
root@192.168.1.28'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@app ~]#
Example: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@app ~]# rsync -avz --max-size='500K' /opt/rpms_db root@192.168.1.28:/tmp
Example: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@gateway ~]# rsync -avzu root@192.168.1.29:/opt/lpicentral /home/pkumar/lpicentral
root@192.168.1.29'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@gateway ~]#
Example: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/pkumar/lpicentral.tgz” is synced to remote system (192.168.1.29), once the synchronization is completed, it will delete the file from source.
[root@gateway ~]# rsync --remove-source-files -zvh /home/pkumar/lpicentral.tgz root@192.168.1.29:/opt
root@192.168.1.29's password:
lpicentral.tgz
sent 597 bytes received 43 bytes 182.86 bytes/sec
total size is 518 speedup is 0.81
[root@gateway ~]#
[root@gateway ~]# ls -l /home/pkumar/lpicentral.tgz
ls: cannot access /home/pkumar/lpicentral.tgz: No such file or directory
[root@gateway ~]#
Example: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@gateway ~]# rsync --dry-run --remove-source-files -zvh /home/pkumar/projects.tar root@192.168.1.29:/opt
root@192.168.1.29'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@gateway ~]#
Example: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@gateway ~]# rsync -avz -e ssh --include '*.pdf *.rpm' --exclude '*.png' /home/pkumar/lpicentral root@192.168.1.29:/opt
root@192.168.1.29's password:
sending incremental file list
lpicentral/
lpicentral/OpenStack-Networking.pdf
lpicentral/httpd-2.4.6-88.el7.centos.x86_64.rpm
lpicentral/httpd-tools-2.4.6-88.el7.centos.x86_64.rpm
lpicentral/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@gateway ~]#
Example: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@gateway ~]# rsync -avz --progress --bwlimit=600 /home/pkumar/lpicentral root@192.168.1.29:/opt
root@192.168.1.29's password:
sending incremental file list
lpicentral/
lpicentral/OpenStack-Networking.pdf
6,153,239 100% 910.02kB/s 0:00:06 (xfr#1, to-chk=6/8)
lpicentral/httpd-2.4.6-88.el7.centos.x86_64.rpm
2,844,028 100% 615.28kB/s 0:00:04 (xfr#2, to-chk=5/8)
lpicentral/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)
lpicentral/pkumar-2.png
0 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=3/8)
lpicentral/pkumar-3.png
0 100% 0.00kB/s 0:00:00 (xfr#5, to-chk=2/8)
lpicentral/pkumar.png
0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=1/8)
lpicentral/postfix-2.10.1-7.el7.x86_64.rpm
2,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@gateway ~]#
Example: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@gateway ~]# rsync -avzi /home/pkumar/lpicentral root@192.168.1.29:/opt
root@192.168.1.29's password:
sending incremental file list
.d..t...... lpicentral/
<f.st...... lpicentral/projects.txt
sent 438 bytes received 45 bytes 138.00 bytes/sec
total size is 11,648,064 speedup is 24,116.07
[root@gateway ~]#
As per above command output, there is difference in file called “projects.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