Skip to main content

Understanding and Utilizing the `rsync` Command in Linux

What Does Synchronizing Files Mean?

Synchronizing files, in the context of computing, refers to the process of ensuring that two or more locations contain an identical set of files and folders.

This process is crucial for data backup, replication for redundancy, or simply for keeping a consistent set of files across multiple devices or locations. The synchronization is not just about copying files, but also about doing it efficiently, transferring only the changes made rather than the entire files afresh.

The rsync Command

rsync (Remote Synchronization) is a versatile file-copying tool in Linux that synchronizes files and directories from one location to another while minimizing data transfer by using delta encoding when appropriate. It's commonly used for backups and mirroring or just copying files with various conditions and filtering.

Syntax of the rsync Command

The basic syntax for the rsync command is:

rsync [options] source destination
  • [options]: These are the flags that control the behavior of rsync.
  • source: The path to the source file(s) or directory.
  • destination: The path where the files will be copied to.

Creating Example Files for Synchronization

Before we proceed with examples, let's create some files and directories that we will use with rsync:

mkdir ~/rsync-source
mkdir ~/rsync-destination
echo "This is a test file." > ~/rsync-source/testfile1.txt
echo "This is another test file." > ~/rsync-source/testfile2.txt

Now we have a directory ~/rsync-source with two test files that we'll sync to ~/rsync-destination.

Examples of Using the rsync Command

Basic File Synchronization

To synchronize all files from the source to the destination directory, use:

rsync -av ~/rsync-source/ ~/rsync-destination

This command syncs the contents of rsync-source to rsync-destination, -a stands for 'archive' mode, which ensures permissions, timestamps, and other file attributes are preserved, and -v enables verbose mode.

Dry Run

Before actually performing the sync, you can perform a dry run to see what changes would be made:

rsync -avn ~/rsync-source/ ~/rsync-destination

The -n option tells rsync to perform a trial run that makes no changes.

Synchronizing Over SSH

For syncing files to a remote server securely over SSH:

rsync -avz -e ssh ~/rsync-source/ user@remotehost:/path/to/destination

Here, -z enables compression during transfer, and -e ssh specifies using SSH for the data transfer.

Excluding Files

If you want to exclude files from being synchronized:

rsync -av --exclude 'testfile2.txt' ~/rsync-source/ ~/rsync-destination

This command will sync all files except testfile2.txt.

Table of rsync Command Options

OptionShorthandDescription
--archive-aArchive mode; equals -rlptgoD (no -H,-A,-X)
--verbose-vVerbose mode; increases the amount of information shown
--compress-zCompress file data during the transfer
--dry-run-nPerform a trial run with no changes made
--excludeExclude files matching pattern
--deleteDelete extraneous files from destination dirs
--progressShow progress during transfer
--rsh=COMMAND-eSpecify the remote shell to use
--recursive-rRecurse into directories
--times-tPreserve modification times
--links-lCopy symlinks as symlinks
--perms-pPreserve permissions
--owner-oPreserve owner (super-user only)
--group-gPreserve group
--devices-DPreserve device files (super-user only)
--specialsPreserve special files

With these examples and the table of options, you can use rsync to efficiently sync files between directories and even across different machines. `rsyncs

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle