How To Use Rsync Command in Linux 16 Practical Examples
How To Use Rsync Command in Linux 16 Practical Examples
com /rsync-local-remote-file-synchronization-commands/
Rsync (Remote Sync) is the most commonly used command for copying and synchronizing files and
directories remotely as well as locally in Linux/Unix systems.
With the help of the rsync command, you can copy and synchronize your data remotely and locally
across directories, disks, and networks, perform data backups, and mirror between two Linux machines.
This article explains 16 basic and advanced uses of the rsync command to transfer your files remotely
and locally in Linux-based machines. You don’t need to be a root user to run the rsync command.
Efficient File Transfer – rsync uses a delta transfer algorithm, which means it only transfers the
differences between source and destination files, which significantly reduces the amount of data
transferred, making it efficient for syncing large files or directories.
Remote File Synchronization – rsync supports both local and remote file transfers over SSH,
which allows synchronization between local and remote systems or mirroring directories across
multiple machines.
Incremental Backups – rsync is well-suited for incremental backups, as it create and update
backups efficiently by transferring only new or modified files.
Preserves File Permissions – rsync can preserve various file attributes, such as permissions,
ownership, timestamps, and symbolic links, which ensures that the copied files retain their original
characteristics on the destination.
Bandwidth Control – rsync allows you to limit the bandwidth usage during file transfers, as it
uses compression and decompression method while sending and receiving data on both ends.
Faster – rsync can be faster than scp (Secure Copy) for transferring files, especially when syncing
large directories or when dealing with files that have already been partially transferred or exist on
the destination.
Here’s an explanation of the different components and options used with rsync commands:
1/13
-v – Verbose output, displaying detailed information about the transfer.
-r – copies data recursively (but doesn’t preserve timestamps and permission while transferring
data.
-a – archive mode, which allows copying files recursively and it also preserves symbolic links, file
permissions, user & group ownerships, and timestamps.
-z – Compress files during transfer to reduce network usage.
-h – human-readable, output numbers in a human-readable format.
-P – Show progress during the transfer.
SOURCE – Specifies the source file(s) or directory to be transferred, which can be a local or a remote
location.
DESTINATION – Specifies the destination path where the files or directories will be copied. Similar
to the source, it can be a local path or a remote location.
[ You might also like: How to Sync Files/Directories Using Rsync with Non-standard SSH Port ]
To copy or sync a file locally, you can use the following command that will sync a single file on a local
machine from one location to another location.
Here in this example, a file name backup.tar needs to be copied or synced to /tmp/backups/ folder.
In the above example, you can see that if the destination is not already existed rsync will create a
directory automatically for the destination.
2/13
Rsync Local Files
The following command will transfer or sync all the files from one directory to a different directory in the
same machine.
Here in this example, /root/rpmpkgs contains some rpm package files and you want that directory to
be copied inside /tmp/backups/ folder.
To copy a directory from a local server to a remote server, you can use the following command, which will
sync a directory from a local to a remote machine.
For example, if there is a folder in your local computer “rpmpkgs” that contains some RPM packages
and if you want that local directory’s content sends to a remote server, you can use the following
command.
3/13
# rsync -avzh /root/rpmpkgs root@192.168.0.141:/root/
This command will help you sync a remote directory to a local directory. Here in this example, a directory
/root/rpmpkgs which is on a remote server is being copied into your local computer in
/tmp/myrpms.
root@192.168.0.141's password:
receiving incremental file list
4/13
created directory /tmp/myrpms
rpmpkgs/
rpmpkgs/httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
rpmpkgs/mod_ssl-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
rpmpkgs/nagios-4.4.6-4.el8.x86_64.rpm
rpmpkgs/nagios-plugins-2.3.3-5.el8.x86_64.rpm
With rsync, we can use SSH (Secure Shell) for data transfer, using SSH protocol while transferring our
data you can be ensured that your data is being transferred in a secured connection with encryption so
that nobody can read your data while it is being transferred over the wire on the internet.
[ You might also like: How to Secure and Harden OpenSSH Server ]
Also when we use rsync we need to provide the user/root password to accomplish that particular task,
so using the SSH option will send your logins in an encrypted manner so that your password will be
safe.
To use rsync over SSH, you can use the -e option to specify the remote shell command, which is
typically ssh as shown.
To synchronize a file from a remote server to a local server, you can specify a protocol with rsync using
the “-e” option with the protocol name you want to use.
Here in this example, We will be using the “ssh” with the “-e” option and perform data transfer.
5/13
root@192.168.0.141's password:
receiving incremental file list
anaconda-ks.cfg
To synchronize a file from a local server to a remote server using SSH, you can leverage the following
command as shown.
root@192.168.0.141's password:
sending incremental file list
created directory /backups
backup.tar.gz
[ You might also like: How to Use Rsync to Sync New or Changed/Modified Files in Linux ]
To show the progress while transferring the data from one machine to a different machine, we can use
the ‘--progress' option, which displays the files and the time remaining to complete the transfer.
6/13
# rsync -avzhe ssh --progress /root/rpmpkgs root@192.168.0.141:/root/rpmpkgs
root@192.168.0.141's password:
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
1.47M 100% 31.80MB/s 0:00:00 (xfr#1, to-chk=3/5)
rpmpkgs/mod_ssl-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
138.01K 100% 2.69MB/s 0:00:00 (xfr#2, to-chk=2/5)
rpmpkgs/nagios-4.4.6-4.el8.x86_64.rpm
2.01M 100% 18.45MB/s 0:00:00 (xfr#3, to-chk=1/5)
rpmpkgs/nagios-plugins-2.3.3-5.el8.x86_64.rpm
120.48K 100% 1.04MB/s 0:00:00 (xfr#4, to-chk=0/5)
To include specific files or patterns during a rsync operation, you can use the --include option with an
extension that matches all files.
In the provided example, rsync will include only files with the .txt extension from the
/path/to/source/ directory during the transfer.
Similarly, to exclude a specific extension during a rsync operation, you can use the --exclude option
with a wildcard pattern.
7/13
# rsync -avz --exclude='*.ext' /path/to/source/
user@remote:/path/to/destination/
In the provided example, rsync will exclude files with the specified extension (*.ext) during the
transfer, while including all other files and directories.
To include and exclude specific files or patterns during a rsync operation, you can use both the --
include and --exclude options with appropriate wildcard patterns.
These two options allow us to include and exclude files by specifying parameters these option helps us
to specify those files or directories which you want to include in your sync and exclude files and folders
with which you don’t want to be transferred.
Here in this example, the rsync command will include those files and directories only which starts with
‘R’ and exclude all other files and directories.
If a file or directory does not exist at the source, but already exists at the destination, you might want to
delete that existing file/directory at the target while syncing.
We can use the ‘--delete‘ option to delete files that are not there in the source directory.
The source and target are in sync. Now create a new file test.txt at the target.
[root@tecmint:~]# cd /root/rpm/
[root@tecmint:~/rpm]# touch test.txt
[root@tecmint:~/rpm]# rsync -avz --delete root@192.168.0.151:/var/lib/rpm/
/root/rpm/
root@192.168.0.151's password:
receiving incremental file list
deleting test.txt
./
8/13
.dbenv.lock
.rpm.lock
Basenames
Conflictname
Dirnames
Enhancename
Filetriggername
Group
Installtid
Name
Obsoletename
Packages
Providename
Sha1header
Sigmd5
Suggestname
Supplementname
Transfiletriggername
Triggername
__db.001
__db.002
__db.003
Target has the new file called test.txt when synchronizing with the source with the ‘–delete‘ option, it
removed the file test.txt.
9/13
Rsync Delete Option
You can specify the Max file size to be transferred or synced. You can do it with the “--max-size”
option. Here in this example, the Max file size is 200k, so this command will transfer only those files
which are equal to or smaller than 200k.
root@192.168.0.151's password:
sending incremental file list
created directory /root/tmprpm
./
.dbenv.lock
.rpm.lock
Conflictname
Enhancename
Filetriggername
Group
Installtid
Name
Obsoletename
10/13
Recommendname
Requirename
Sha1header
Sigmd5
Suggestname
Supplementname
Transfiletriggername
Triggername
__db.002
Now, suppose you have the main web server and a data backup server, you created a daily backup and
synced it with your backup server, but now you don’t want to keep that local copy of the backup in your
web server.
So, will you wait for the transfer to complete and then delete that local backup file manually? Of Course
NO. This automatic deletion can be done using the ‘--remove-source-files‘ option.
11/13
root@192.168.0.151's password:
backup.tar.gz
[root@tecmint:~]# ls -l backup.tar.gz
If you are a newbie using rsync and don’t know what exactly your command going to do. Rsync could
really mess up the things in your destination folder and then doing an undo can be a tedious job.
[ You might also like: How to Sync Two Apache Web Servers/Websites Using Rsync ]
Use of this option will not make any changes to the files and shows the output of the command, if the
output shows exactly the same as you want to do then you can remove the ‘--dry-run‘ option from your
command and run on the terminal.
root@192.168.0.151's password:
backup.tar.gz
12/13
Rsync Dry Run
You can set the bandwidth limit while transferring data from one machine to another machine with the
help of ‘--bwlimit‘ option. This option helps us to limit I/O bandwidth.
Also, by default rsync syncs changed blocks and bytes only, if you explicitly want to sync the whole file
then you use the ‘-W‘ option with it.
Conclusion
That concludes our overview of rsync and its capabilities. For further exploration of its extensive options
and functionalities, I encourage you to refer to the comprehensive manual pages (man pages) available.
13/13