Bash Wiki
Posts (Latest 30 updated) :
Read all
Contents:
  1. [Linux] Bash rsync Uso: Synchronize files and directories efficiently
    1. Overview
    2. Usage
    3. Common Options
    4. Common Examples
    5. Tips

[Linux] Bash rsync Uso: Synchronize files and directories efficiently

Overview

The rsync command is a powerful tool used for synchronizing files and directories between two locations, either on the same machine or across different machines. It is particularly useful for backups and mirroring, as it only transfers the differences between the source and the destination, making it efficient in terms of both speed and bandwidth.

Usage

The basic syntax of the rsync command is as follows:

rsync [options] [source] [destination]

Common Options

Here are some commonly used options with rsync:

  • -a: Archive mode; it preserves permissions, timestamps, symbolic links, and other attributes.
  • -v: Verbose; provides detailed output of the transfer process.
  • -z: Compress; compresses file data during the transfer for faster transmission.
  • -r: Recursive; copies directories recursively.
  • --delete: Deletes files in the destination that are not present in the source.
  • -n: Dry run; shows what would be done without actually making any changes.

Common Examples

  1. Basic file synchronization:
    rsync -av source.txt destination.txt
    
  2. Synchronizing a directory:
    rsync -av /path/to/source/ /path/to/destination/
    
  3. Synchronizing with compression:
    rsync -avz /path/to/source/ user@remote_host:/path/to/destination/
    
  4. Deleting files in the destination that are not in the source:
    rsync -av --delete /path/to/source/ /path/to/destination/
    
  5. Performing a dry run to see what would happen:
    rsync -avn /path/to/source/ /path/to/destination/
    

Tips

  • Always use the -n option for a dry run before executing a command that modifies files, especially when using --delete.
  • When transferring large files or directories, consider using the -z option to speed up the process by compressing the data.
  • Be cautious with the trailing slashes in paths; a trailing slash on the source directory means “copy the contents of this directory,” while omitting it means “copy the directory itself.”