Rsync Command in Linux with Examples _ Linuxize (2)
Rsync Command in Linux with Examples _ Linuxize (2)
rsync is a fast and versatile command-line utility for synchronizing files and
directories between two locations over a remote shell, or from/to a remote Rsync
daemon. It provides fast incremental file transfer by transferring only the differences
between the source and the destination.
Rsync can be used for mirroring data, incremental backups, copying files between
systems, and as a replacement for scp , sftp , and cp commands.
This article explains how to use rsync through practical examples and detailed
explanations of the most common rsync options.
Installing Rsync
The rsync utility is pre-installed on most Linux distributions and macOS. If you
don’t have rsync installed on your system, you can easily install it using your
distribution’s package manager.
https://linuxize.com/post/how-to-use-rsync-for-local-and-remote-data-transfer-and-synchronization/ 1/8
11/9/24, 12:54 PM Rsync Command in Linux with Examples | Linuxize
rsync provides a number of options that control how the command behaves. The
most widely used options are:
https://linuxize.com/post/how-to-use-rsync-for-local-and-remote-data-transfer-and-synchronization/ 2/8
11/9/24, 12:54 PM Rsync Command in Linux with Examples | Linuxize
--delete . When this option is used, rsync deletes extraneous files from the
destination location. It is useful for mirroring.
-e . This option allows you to choose a different remote shell. By default, rsync
is configured to use ssh.
The user running the command must have read permissions on the source location
and write permissions on the destination.
Omitting the filename from the destination location copies the file with the current
name. If you want to save the file under a different name, specify the new name on
the destination part:
The real power of rsync comes when synchronizing directories. The example below
shows how to create a local backup of website files:
https://linuxize.com/post/how-to-use-rsync-for-local-and-remote-data-transfer-and-synchronization/ 3/8
11/9/24, 12:54 PM Rsync Command in Linux with Examples | Linuxize
It is worth mentioning that rsync gives different treatment to the source directories
with a trailing slash ( / ). If the source directory has a trailing slash, the command will
copy only the directory contents to the destination directory. When the trailing slash
is omitted, rsync copies the source directory inside the destination directory.
If you haven’t set a passwordless SSH login to the remote machine, you will
be asked to enter the user password.
To transfer data from a remote to a local machine, use the remote location as a
source:
If SSH on the remote host is listening on a port other than the default 22 , specify
the port using the -e option:
https://linuxize.com/post/how-to-use-rsync-for-local-and-remote-data-transfer-and-synchronization/ 4/8
11/9/24, 12:54 PM Rsync Command in Linux with Examples | Linuxize
When excluding files or directories , you need to use their relative paths to the
source location.
In the following example shows how exclude the node_modules and tmp directories:
The second option is to use the --exclude-from option and specify the files and
directories you want to exclude in a file.
/exclude-file.txt
node_modules
tmp
Conclusion
https://linuxize.com/post/how-to-use-rsync-for-local-and-remote-data-transfer-and-synchronization/ 5/8
11/9/24, 12:54 PM Rsync Command in Linux with Examples | Linuxize
We have shown you how to use Rsync to copy and synchronize files and directories.
There’s lots more to learn about Rsync at Rsync User’s Manual page.
rsync terminal
BUY ME A COFFEE
Sign up to our newsletter and get our latest tutorials and news
straight to your mailbox.
Related Articles
AUG 19, 2019
How to Transfer Files with Rsync over SSH
https://linuxize.com/post/how-to-use-rsync-for-local-and-remote-data-transfer-and-synchronization/ 6/8
11/9/24, 12:54 PM Rsync Command in Linux with Examples | Linuxize
https://linuxize.com/post/how-to-use-rsync-for-local-and-remote-data-transfer-and-synchronization/ 7/8
11/9/24, 12:54 PM Rsync Command in Linux with Examples | Linuxize
https://linuxize.com/post/how-to-use-rsync-for-local-and-remote-data-transfer-and-synchronization/ 8/8