The Ultimate Guide to Using Rsync for Efficient File Transfer

abhiramTS
4 min readMar 27, 2023

--

Have you ever found yourself frustrated while transferring files from one directory to another? I certainly have! Using the cp command is quick, but it lacks a progress indicator to show you how much of the file has been transferred and how much time is remaining. Fortunately, I discovered rsync, a powerful and versatile tool that not only shows progress but also offers many more features. In this article, I'll explain what rsync is, how to install it on various platforms, and highlight its features.

What is rsync?

Rsync is a command-line tool that allows you to transfer and synchronize files between two directories, either on the same machine or between different machines. It was created by Andrew Tridgell and Paul Mackerras in 1996 and has since become an essential tool for system administrators and anyone who works with large files.

Installing rsync

Installing rsync is relatively straightforward on most platforms. Here are some examples of how to install it on various systems:

On Ubuntu and Debian:

sudo apt-get update sudo apt-get install rsync

On Fedora:

sudo dnf install rsync

On Arch Linux:

sudo pacman -S rsync

Rsync flags explained

Rsync offers a variety of flags that can be used to tailor the transfer process to your needs. Here are some of the most commonly used flags:

  • -v: Verbose output, which provides detailed information about what rsync is doing during the transfer.
  • -h: Human-readable output, which shows file sizes in a more readable format (e.g., "1.2M" instead of "1234567").
  • -r: Recursive transfer, which allows rsync to copy directories and their contents.
  • -a: Archive mode, which preserves permissions, ownership, timestamps, and symbolic links.
  • -z: Compress transfer, which compresses the data during transfer to reduce network traffic.
  • -P: Progress indicator, which shows the progress of the transfer, including the amount of data transferred and the estimated time remaining.

Examples of rsync in action

Here are some examples of how to use rsync with various flags:

Copy a file from one directory to another:

rsync /path/to/source/file /path/to/destination/

This example shows how to use rsync to copy a file from one directory to another. Replace “/path/to/source/file” with the path to the file you want to copy, and “/path/to/destination/” with the path to the directory you want to copy the file to.

Copy a directory and its contents:

rsync -r /path/to/source/directory /path/to/destination/

This example shows how to use rsync to copy a directory and its contents to another directory. Replace “/path/to/source/directory” with the path to the directory you want to copy, and “/path/to/destination/” with the path to the directory you want to copy the directory to.

Copy a directory and its contents with archive mode:

rsync -a /path/to/source/directory /path/to/destination/

This example shows how to use rsync with the archive mode flag (-a) to copy a directory and its contents while preserving permissions, ownership, timestamps, and symbolic links. Replace “/path/to/source/directory” with the path to the directory you want to copy, and “/path/to/destination/” with the path to the directory you want to copy the directory to.

Copy a directory and its contents with a progress indicator:

rsync -avzP /path/to/source/directory /path/to/destination/

This example shows how to use rsync with multiple flags (-avzP) to copy a directory and its contents while showing a progress indicator, compressing the data during transfer, and preserving permissions, ownership, timestamps, and symbolic links. Replace “/path/to/source/directory” with the path to the directory you want to copy, and “/path/to/destination/” with the path to the directory you want to copy the directory to.

Exclude files from being copied:

You can use the --exclude flag to exclude specific files or directories from being copied. For example:

rsync -av --exclude="*.log" /path/to/source/ /path/to/destination/

This will copy all files and directories in the source directory to the destination directory, except for any files with the “.log” extension.

Delete files from the destination that don’t exist in the source

You can use the --delete flag to delete any files or directories in the destination directory that don't exist in the source directory. For example:

rsync -av --delete /path/to/source/ /path/to/destination/

This will copy all files and directories in the source directory to the destination directory, and also delete any files or directories in the destination directory that don’t exist in the source directory.

Sync directories recursively:

You can use the -r flag to sync directories and their contents recursively. For example:

rsync -avz /path/to/source/ /path/to/destination/

This will copy all files and directories in the source directory to the destination directory, and also sync any subdirectories and their contents.

Limit bandwidth usage during the transfer:

You can use the --bwlimit flag to limit bandwidth usage during transfer. For example:

rsync -avz --bwlimit=500 /path/to/source/ /path/to/destination/

This will copy all files and directories in the source directory to the destination directory, limiting the transfer speed to 500 kilobytes per second.

Dry-run (test) mode

You can use the -n flag to perform a dry-run, which shows what rsync would do without actually copying any files. For example:

rsync -avn /path/to/source/ /path/to/destination/

This will show what files would be copied and what changes would be made, but won’t copy any files.

These are just a few examples of the many different flags you can use with rsync.

Conclusion

In conclusion, rsync is a powerful and versatile tool that makes file transfers and synchronization more manageable and efficient. With rsync, you can monitor the progress of your transfers, copy directories and their contents, and preserve permissions, ownership, timestamps, and symbolic links. I hope this article has been helpful, and I encourage you to give rsync a try!

--

--