The rsync command is a powerful linux tool which can be used to easily copy files between servers or within a local device. In this tutorial, we’ll show you how to use rsync on Linux.
What is Rsync?
Rsync is a file transfer command on the Linux operating system. It’s used to copy a file list or folders from one source to another. This can be between two locations on a local device, or between a local and remote server. The rsync command is often used as a basic server backup tool, where a cron job is set up to copy the files on a local server to a remote server periodically.
How to use Rsync
Seeing a Rsync example is the easiest way to get started using the Rsync command. Here are some common scenarios you may wish to be familiar with:
How to install Rsync
In order to begin using rsync, you may need to install it. Follow these commands:
Ubuntu and Debian
sudo apt install rsync
CentOS and Fedora
sudo yum install rsync
Viewing the Rsync Manual
If you’re ever lost while using rsync, the man command is there for you to view the quick start manual:
There are a variety of rsync options can may be used with each command:
- -r: recurisvely transfer file, required for syncing an entire directory
- -a: Archive function which preserves symbolic links, file owners, and permissions.
- –delete: Removes the files from the destination directory if they have been removed from the source destination. This means it will remove source files once they have been deleted in the source folder, which is not done by default.
- –exclude: Allows you to exclude files based on a pattern. Example: -eclude=cat would exclude source files with the word cat in them.
- –n: dry run to test and see what actions the rsync will perform without actually running the rsync command. The dry run is really useful the first time you run a potentially impactful operation.
- –v: verbose output of the process
- –P: Adds a progress bar to show you how far along the transfer process is.
- –z: Adds compression to the file transfer process to reduce the max size of the transferred files.
Synchronize Two Directories On the Same System
To sync two directories, we can use this command with the -a flag which will preserve file ownership and permissions.
This command will copy the files from folder1 and place them in folder2.
The trailing slash after the first folder will place the files from inside of folder 1 into folder2 rather than placing the folder itself in folder2:
rsync -a folder1/ folder2
Synchronize To A Directory On A Remote Host
Transfer Files to Remote Host With Rsync
To push a set of files on the server you are currently logged into, start by exchanging SSH keys with the server (for a passwordless transfer) and then use this command:
rsync -a ~/dir1 [email protected]_host:destination_directory
It will copy the file list on the local device in source destination folder1 to the directory specified directory on the remote host.
Transfer Files From Remote Machine With Rsync
If you wish to transfer files from a remote machine to the local server, the following command may be used:
rsync -a [email protected]_host:/home/username/dir1 /local/folder
This will transfer the file list from the remote machine in dir1 and make a copy in /local/folder
Deploy A New Cloud or Dedicated Server Today
Now that you’ve seen some rsync examples, you may be in the market for a new Cloud or Dedicated Server to test it out on. ServerMania offers Cloud servers starting at just $5 per month and Dedicated Servers which can be deployed in as little as 8 hours. Click the links below to learn more about each product.
COMPLETE DIGITAL SERVER SOLUTIONS FOR ALL
Bare Metal Dedicated Servers
A single tenant, physical server allowing you full access to its resources.Browse servers
Professional Hybrid Servers
Virtualized server platform hosted on enterprise-grade physical servers.
Scalable Cloud Servers
High-performance and highly-available infrastructure.Browse servers
Our next-generation data center facilities.Browse servers