DEV Community

saeedeldeeb
saeedeldeeb

Posted on

The Power of Rsync: Efficient File Synchronization for Linux

Rsync is a powerful utility for file synchronization in Linux that allows you to efficiently copy and synchronize files between local and remote locations. In this article, we'll explore some of the most commonly used rsync commands and their practical applications.

Basic File Copying

  1. Copy a File from Source to Destination:

To copy a single file from the source to the destination, use the following command:

   $ rsync source/photos.zip destination
Enter fullscreen mode Exit fullscreen mode
  1. Copy a Folder Recursively:

To copy a folder and its contents recursively, use the -r option:

   $ rsync -r source/ destination
Enter fullscreen mode Exit fullscreen mode
  1. Copy Files with Timestamp Preservation:

The -a option (archive mode) preserves file permissions, timestamps, and more during the copy operation:

   $ rsync -a source/ destination
Enter fullscreen mode Exit fullscreen mode

Monitoring Progress

  1. List Copying Progress:

For a more verbose output that lists the files being copied and the progress, use the -v (verbose) option:

   $ rsync -av source/ destination
Enter fullscreen mode Exit fullscreen mode

Alternatively, use the --progress option for a human-readable progress indicator:

   $ rsync -a --progress source/ destination
Enter fullscreen mode Exit fullscreen mode
  1. Human-Readable Progress:

For a human-readable progress display, add the -h option:

   $ rsync -ah --progress source/ destination
Enter fullscreen mode Exit fullscreen mode

Handling Interruptions

  1. Partial Copies for Large Files:

When dealing with large files, use the --partial option to allow for partial copies. This enables you to resume interrupted transfers:

   $ rsync -ah --progress --partial source/ destination
Enter fullscreen mode Exit fullscreen mode

Alternatively, the -P option is a shorthand for --partial --progress.

Advanced Synchronization

  1. Syncing with Deletion:

To sync files and delete items in the destination that no longer exist in the source, add the --delete option:

   $ rsync -ahP --delete source/ destination
Enter fullscreen mode Exit fullscreen mode
  1. Remove Source Files after Successful Copy:

To automatically delete files from the source after a successful copy, use the --remove-source-files option:

   $ rsync -ahP --remove-source-files source/ destination
Enter fullscreen mode Exit fullscreen mode

Note: This won't delete source folders, so an additional step is required, you can use $ find like -type d -empty -delete

Remote Synchronization

  1. Synchronizing with a Remote Server:

To sync files from a local machine to a remote server, use the following syntax:

   $ rsync -ahP source root@your.ip:path/to/folder
Enter fullscreen mode Exit fullscreen mode
  1. Archiving Before Sync:

    Archiving can be useful to preserve attributes and reduce the amount of data transferred. To archive a remote folder before syncing, use:

    $ rsync -ahPz root@your.ip:path/to/folder source/
    

    If the SSH port is non-default (e.g., 22), specify it using the -e option:

    $ rsync -ahPze 'ssh -p 22' root@your.ip:path/to/folder source/
    

Rsync is a tool for efficiently managing file synchronization tasks on Linux systems. With its flexibility and numerous options, it empowers users to handle a wide range of data synchronization requirements with ease.

Top comments (0)