Atlantic.Net Blog

Rsync Command in Linux with Examples

Hitesh Jethva
by Atlantic.Net (228 posts) under VPS Hosting
0 Comments

rsync is a command-line utility for Unix-based operating systems that can be used to transfer and synchronize files and directories between multiple computers. It is an extraordinarily fast file copying tool that uses an algorithm to transfer only the portions of files that have changed.

rsync is written in C and is a replacement for scp, sftp, and cp commands.

rsync uses compression and decompression of data blocks, which will save your bandwidth compared to other file transfer protocols, like FTP. It also encrypts the data during the file transfer using SSH protocol.

In this tutorial, we will show you how to use rsync command to transfer files and directories in Linux.

Prerequisites

  • A fresh Ubuntu 20.04 VPS on the Atlantic.Net Cloud Platform
  • A root password configured on your server

Create an Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing Ubuntu 20.04 as the operating system with at least 1GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged in to your Ubuntu 20.04 server, run the following command to update your base system with the latest available packages.

apt-get update -y

Install rsync

rsync comes pre-installed in most Linux operating systems. If not installed, you can install it with the following command:

apt-get install rsync -y

Once installed, you can verify the version of rsync using the following command:

rsync --version

You should see the following output:

Rsync Syntax

The basic syntax of rsync command is shown below:

rsync [OPTION] [SOURCE] [DESTINATION]

rsync comes with a lot of options. A brief explanation of each option is shown below:

-a: Used to sync directories recursively and preserve symbolic links, modification times, group, ownership, and permissions.

-r: Used to sync directories recursively but does not keep ownership for users and groups, permissions, timestamps, or symbolic links.

-z: Used to compress the data during the transfer.

-e: Used to specify SSH protocol for remote transfers.

-h: Used to print the numbers in the output in a human-readable format.

-b: Used to take a backup during synchronization.

1. Copy a File Locally

To copy a single file from one directory to another on the local system, use the following command:

rsync -v /etc/rsyslog.conf /mnt/

This will copy a file named rsyslog.conf located in /etc to the /mnt directory:

To copy multiple files from one directory to another on the local system, use the following command:

rsync -v /etc/fstab /etc/passwd /mnt

This will copy files fstab and passwd to the /mnt directory:

rsync -v /etc/fstab /etc/passwd /mnt

2. Copy a Directory Locally

To copy a directory with all its contents to the other directory on the local system, use the following command:

rsync -av /etc/init.d /mnt

This will copy directory named init.d and all its contents to the /mnt directory:

3. Copy Files and Directories from the Local to the Remote System

With rsync, you can also copy or sync files and directories from the local to the remote system.

For example, to copy the directory /etc on the local system to the remote system (192.168.0.102) under the /mnt directory, run the following command:

rsync -zarvh /etc/init.d/ [email protected]:/mnt/

You will be asked to provide the root password of remote system:

4. Copy Files and Directories from the Remote to the Local System

If you want to copy the directory /etc/rcS.d from the remote system (192.168.0.102) to the local system inside /opt directory, run the following command:

rsync -zarvh [email protected]:/etc/rcS.d /opt/

You should see the following output:

5. Display Synchronization Progress During Transfer

You can also display the file transfer progress using the option –progress. You can run the following command to display the progress bar:

rsync -avh --progress [email protected]:/etc/apache2 /opt/

6. Limit Bandwidth During Transfer

You can also set the bandwidth limit for data transfer using the option –bwlimit.

For example, set the bandwidth limit to 500 KB/s during file transfer using the following command:

rsync -avz --progress --bwlimit=500 /root/ubuntu-20.04-live-server-amd64.iso
[email protected]:/mnt/

7. Include and Exclude Files During Transfer

In some cases, you want to include or exclude only specific file types during the transfer. In this case, you can use –include and –exclude option.

rsync -avz --include '*.php' --exclude *.png /var/www/html/wordpress /opt/

This will copy only PHP files and exclude PNG files from /var/www/html/wordpress to /opt directory.

Conclusion

In the above guide, you learned how to use rsync command to transfer files from one system to another. You also learned how to use different options with rsync command. You can now easily use the rsync command to perform backup tasks; try using rsync on your VPS Hosting account from Atlantic.Net!

Get A Free To Use Cloud VPS

Free Tier Includes:
G3.2GB Cloud VPS Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


Looking for a Hosting Solution?

We Provide Cloud, Dedicated, & Colocation.

  • Seven Global Data Center Locations.
  • Flexible Private, Public, & Hybrid Hosting.
  • 24x7x365 Security, Support, & Monitoring.
Contact Us Now! Med Tech Award FTC
SOC Audit HIPAA Audit HITECH Audit

Recent Posts

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G3.2GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2323 Bryan Street,

Dallas, Texas 75201

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4

Canada

London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom

Resources