Home > File & Directory Management > rsync-dry-run

rsync-dry-run: Previewing rsync Operations

rsync-dry-run is a feature that simulates the execution of an rsync command without actually performing any file transfers or modifications. It shows you which files would be copied, deleted, or changed if the rsync command were run. This is typically implemented using the 'rsync -n' or 'rsync --dry-run' option. It's a crucial safety measure to prevent unexpected results before handling important data.

Overview

The dry-run mode of rsync simulates the effects of a command before executing it, providing users with a preview of the changes. This is particularly important when running complex rsync commands that include destructive operations, such as those using the `--delete` option.

Key Benefits

  • Prevent Data Loss: Detect unintended file deletions or overwrites in advance.
  • Verify Expected Changes: Accurately understand which files will be transferred or modified.
  • Validate Commands: Confirm that complex rsync option combinations work as intended.
  • Save Time: Filter out unnecessary operations in large-scale transfers beforehand.

Key Options

rsync-dry-run essentially utilizes rsync's options. Here, we describe the main rsync options commonly used in conjunction with the dry-run mode.

Dry-run Related Options

Common rsync Options (Used with Dry-run)

Generated command:

Try combining the commands.

Description:

`rsync-dry-run` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Various scenarios demonstrating the use of rsync's dry-run mode. All examples output results without making actual changes.

Basic Dry-run

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

Preview which files will be transferred when synchronizing the contents of a source directory to a destination directory.

Preview with Delete Option

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

Preview which files will be deleted if files only present in the destination directory are to be removed. This is a critical step.

Dry-run to a Remote Server

rsync -avn /path/to/local/ user@remote_host:/path/to/remote/

Preview which files will be transferred before sending files to a remote server.

Dry-run with Exclude and Include Patterns

rsync -avn --exclude='*.log' --include='data/' --include='*/' --exclude='*' /path/to/source/ /path/to/destination/

Preview how complex filtering rules, such as excluding specific file extensions and including only certain directories, will be applied.

Tips & Precautions

Tips and precautions for effectively using rsync's dry-run feature.

Always Use Dry-run First

Especially when using options like `--delete` or complex filtering rules, always use the `-n` option to check the results before executing the actual rsync command. This is the best way to prevent irreversible data loss.

  • **Safety First:** Always start with `rsync -avn ...` when synchronizing important data.
  • **Analyze Output:** Carefully review the dry-run output to ensure there are no unexpected discrepancies.

Check rsync Installation

rsync is installed by default on most Linux systems. If it's not, you'll need to install it using your package manager (e.g., `sudo apt install rsync` or `sudo yum install rsync`). Since rsync-dry-run uses rsync commands, rsync must be installed.

Importance of Trailing Slashes

Whether you include a trailing slash (/) at the end of the source path affects rsync's behavior. Dry-run helps you clearly understand this difference.

  • `source/`: Copies the *contents* of the source directory to the destination directory.
  • `source`: Copies the source directory *itself* into the destination directory.

Same category commands