This post explains 17 useful rsync command examples in Linux. These examples will empower you to manage your data effortlessly in a Linux environment.
Rsync Command
In the world of Linux, when it comes to efficiently synchronizing and transferring data between local and remote systems, Rsync stands as a powerhouse utility. Rsync, short for “remote sync,” is a versatile tool that not only simplifies file synchronization but also optimizes data transfers. 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.
Additionally, 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.
Rsync Installation
Rsync command is available for all UNIX and Linux like operating systems. When we do minimal installation of RHEL based distros then rsync package is not part of default installation, so to install rsync, run
$ sudo dnf install rsync -y
For Debian Like operating Systems (Ubuntu & Linux Mint) use below apt command to install rysnc tool,
$ sudo 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>
Options in rsync command
- -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
1) Basic Local Copy
Let’s assume we want to copy a file from user’s home directory to /opt/back folder, run
# 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
#
In above we have used the options like -z for compression, -v for verbose output and -h for human readable output.
2) Copy / Sync Directory Locally
Assume we want to copy user’s home directory to /opt/backup folder, execute beneath command
# 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
#
Let’s verify whether directory is copied or not, execute below ls command
# 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
#
As we can see “pkumar” directory is created on destination folder and its contents are also copied.
Note: In above example if use trail / after source folder like ‘/home/pkumar/’ then rsync command will not create pkumar directory on destination but it will copy the content only.
# rsync -zavh /home/pkumar/ /opt/backup
3) Copy Files & Directories Recursively Locally
Using the option like -a or -r, it can copy files and directories recursively.
Note : In rsync command -a option is used for archiving during the copy 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
# rsync -zrvh /home/pkumar /opt/backup
or
# rsync -zavh /home/pkumar /opt/backup
4) Copy Files From Local to Remote System
Let’s suppose we want to copy the folder “/home/pkumar/techi” from local machine to remote machine (192.168.1.29) under the /opt folder.
# rsync -zavh /home/pkumar/techi [email protected]:/opt [email protected]'s password: ………………………………………………………… techi/otrs-community-edition-6.0.40/var/stats/Stats.NewTickets.de.xml techi/otrs-community-edition-6.0.40/var/stats/Stats.NewTickets.en.xml techi/otrs-community-edition-6.0.40/var/stats/Stats.NewTickets.hu.xml techi/otrs-community-edition-6.0.40/var/stats/Stats.StatusActionOverview.de.xml techi/otrs-community-edition-6.0.40/var/stats/Stats.StatusActionOverview.en.xml techi/otrs-community-edition-6.0.40/var/stats/Stats.StatusActionOverview.hu.xml techi/otrs-community-edition-6.0.40/var/stats/Stats.TicketOverview.de.xml techi/otrs-community-edition-6.0.40/var/stats/Stats.TicketOverview.en.xml techi/otrs-community-edition-6.0.40/var/stats/Stats.TicketOverview.hu.xml techi/otrs-community-edition-6.0.40/var/tmp/ techi/otrs-community-edition-6.0.40/var/webservices/ techi/otrs-community-edition-6.0.40/var/webservices/examples/ techi/otrs-community-edition-6.0.40/var/webservices/examples/Base.pm sent 21.36M bytes received 47.95K bytes 1.48M bytes/sec total size is 169.29M speedup is 7.91 #
5) Copy Files From Remote System to Local
Pull files from a remote server to your local machine:
# rsync -av user@remote:/path/to/source /path/to/destination
Let’s suppose we want to copy /opt/rpms_db from remote system (192.168.1.29) to our local system under /tmp folder.
# rsync -zavh [email protected]:/opt/rpms_db /tmp [email protected]'s password: receiving incremental file list rpms_db/ rpms_db/nginx-1.20.1-14.el9.x86_64.rpm rpms_db/nginx-core-1.20.1-14.el9.x86_64.rpm rpms_db/nginx-filesystem-1.20.1-14.el9.noarch.rpm rpms_db/redhat-logos-httpd-90.4-1.el9.noarch.rpm sent 104 bytes received 620.94K bytes 248.42K bytes/sec total size is 665.76K speedup is 1.07 #
Above command will automatically create a folder “rpms_db” under the /tmp folder in our local machine.
# 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 # ls -l /tmp/rpms_db/ total 16028 total 656 -rw-r--r--. 1 root root 43721 Oct 2 18:51 nginx-1.20.1-14.el9.x86_64.rpm -rw-r--r--. 1 root root 589537 Oct 2 18:51 nginx-core-1.20.1-14.el9.x86_64.rpm -rw-r--r--. 1 root root 13616 Oct 2 18:51 nginx-filesystem-1.20.1-14.el9.noarch.rpm -rw-r--r--. 1 root root 18891 Oct 2 18:51 redhat-logos-httpd-90.4-1.el9.noarch.rpm #
6) Copying Files Across SSH
Rsync seamlessly integrates with SSH for secure remote file transfers. Use the following syntax:
# rsync -zavh -e ssh /path/to/source user@remote:/path/to/destination
Example
# rsync -zavh -e ssh [email protected]:/opt/rpms_db /tmp [email protected]'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 #
If your SSH server uses a non-default port, specify it with the -e option:
# rsync -av -e 'ssh -p 2222' /path/to/source user@remote:/path/to/destination
7) Show Progress During Transfer
Use “–progress” to display a progress bar to visualize the status of the file transfer:
# rsync -avh --progress /path/to/source /path/to/destination
Example
# rsync -avh --progress [email protected]:/opt/rpms_db /tmp [email protected]'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 #
8) Copy Directory Structure Without Copying Files
There are some scenarios where we want to copy the exact directory structure from local machine to remote or vice versa without copying the files.
Suppose we want to copy the directory structure of “/home/pkumar” from local machine to remote machine (192.168.1.29) under /opt folder.
# rsync -av -f"+ */" -f"- *" /home/pkumar [email protected]:/opt/
[email protected]'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
#
9) Resume Large File Transfer After Getting Failed in SCP
There could be some scenarios in day to dat tasks 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,
# scp [email protected]:/root/ubuntu-22.04-desktop-amd64.iso /opt [email protected]'s password: ubuntu-22.04-desktop-amd64.iso 28% 526MB 61.5MB/s 00:21 ETA ^CKilled by signal 2. # # rsync -P --rsh=ssh [email protected]:/root/ubuntu-22.04-desktop-amd64.iso /opt [email protected]'s password: ubuntu-22.04-desktop-amd64.iso 1,921,843,200 100% 18.47MB/s 0:01:39 (xfr#1, to-chk=0/1) #
10) Delete Files at Destination If Not Present in Source
Remove files at the destination that no longer exist in the source, use –delete option in rsyn command
# rsync -avz --delete /opt/rpms_db [email protected]:/tmp/rpms_db [email protected]'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 #
11) Put Limit on File Transfer 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.
# rsync -avz --max-size='500K' /opt/rpms_db [email protected]:/tmp
12) Synchronize Only Modified Files
Rsync excels at efficiently transferring only modified files using –update or -u option.
# rsync -av --update /path/to/source /path/to/destination
Example,
# rsync -av --update /home/pkumar/techi [email protected]:/opt/techi
13) Remove Files From Source After Synchronization
Let’s suppose you want to delete files from source once the synchronization is completed using rsync command. In the example below, folder from local system “/home/pkumar/techi.tgz” is synced to remote system (192.168.1.29), once the synchronization is completed, it will be deleted from the source.
# rsync --remove-source-files -zvh /home/pkumar/techi.tgz [email protected]:/opt [email protected]'s password: techi.tgz sent 597 bytes received 43 bytes 182.86 bytes/sec total size is 518 speedup is 0.81 # # ls -l /home/pkumar/techi.tgz ls: cannot access /home/pkumar/techi.tgz: No such file or directory #
14) Perform a Dry Run
Preview the changes that rsync will make without actually copying anything:
# rsync -av --dry-run /path/to/source /path/to/destination
Example,
# rsync --dry-run -zvh /home/pkumar/projects.tar [email protected]:/opt [email protected]'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) #
15) Include And Exclude files During Copy
In some situations where we want to copy or sync files & directories of specific type and want to exclude files of specific type. Rsync command supports both include and exclude options.
Below example is copying the files of type pdf and rpm and exclude png file types.
# rsync -avz -e ssh --include '*.pdf *.rpm' --exclude '*.png' /home/pkumar/techi [email protected]:/opt
[email protected]'s password:
sending incremental file list
techi/
techi/OpenStack-Networking.pdf
techi/httpd-2.4.6-88.el7.centos.x86_64.rpm
techi/httpd-tools-2.4.6-88.el7.centos.x86_64.rpm
techi/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
#
16) Limit Bandwidth Usage
In rsync command we can set the bandwidth limit for data transfer from one machine to another usin ‘–bwlimit=<KB/S>‘ option.
Assume we want to set maximum data transfer rate (speed) is 600 KB/s with rsync, example is shown below:
# rsync -avz --progress --bwlimit=600 /home/pkumar/techi [email protected]:/opt
[email protected]'s password:
sending incremental file list
techi/
techi/OpenStack-Networking.pdf
6,153,239 100% 910.02kB/s 0:00:06 (xfr#1, to-chk=6/8)
techi/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)
techi/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)
techi/pkumar-2.png
0 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=3/8)
techi/pkumar-3.png
0 100% 0.00kB/s 0:00:00 (xfr#5, to-chk=2/8)
techi/pkumar.png
0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=1/8)
techi/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
#
17) View the Difference 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
# rsync -avzi /home/pkumar/techi [email protected]:/opt
[email protected]'s password:
sending incremental file list
.d..t...... techi/
<f.st...... techi/projects.txt
sent 438 bytes received 45 bytes 138.00 bytes/sec
total size is 11,648,064 speedup is 24,116.07
#
Above output shows that there is a difference in file called “projects.txt” on destination. Following keywords are used in the output,
- d: indicates change in destination file
- f: indicates a file
- t: indicates change in timestamps
- s: indicates change in size
Additonal Examples
Copy Sparse Files Efficiently
Handle sparse files efficiently with the –sparse option:
# rsync -av --sparse /path/to/source /path/to/destination
Schedule Periodic Sync with Cron
Automate periodic synchronization with cron jobs:
0 2 * * * rsync -av /path/to/source /path/to/destination
Conclusion
Rsync is a remarkable tool that simplifies data synchronization and transfer in Linux, offering a plethora of options and capabilities. These 17 Rsync command examples provide you with a solid foundation for managing your data efficiently. Whether you need to back up your files, synchronize directories, or optimize data transfers, Rsync empowers you to do so with ease in a Linux environment. Incorporate these commands into your toolbox, and you’ll become a master of data synchronization in no time.
Also Read : Tar Command in Linux with Practical Examples
Also Read: How to Rotate and Compress Log Files in Linux with Logrotate
very helpful.
thanks
I was looking for a way to copy a folder with files worth 155 GB in Linux. This is exactly what I was looking for. Thanks for taking the time to write this article with detailed examples.
Thanks, using this article I create a simple script to copy my data from Unraid server to a Synology NAS.
Please be advised that the -z flag for compression only compresses the files during the transfer and then uncompresses them at the destination, so it’s totally unnecessary when using rsync locally or even inside a gigabit network.
Hi, could you explain the use of “p” in “-a equal to following options -rlptgoD”?
Found it: -p, –perms preserve the permissions.
I have used rsync for years and always avoided using -z for copies on the same system.
I have assumed compressing it then uncompressing it would only add CPU overhead since the limiting factors would be the read and write speeds of the disk(-s) involved. Am I missing something?
Hi Jacob,
You are correct when we are dealing with large file or huge file.Rsync with -z option can add CPU overhead.
If you want to sync your site on another server do this:
Notice the slash at the end of path
rsync -zarvh -P /var/www/sire.com root@{Remote-Server-IP-Address}:/var/www/