Overview
`rsync --delete` copies the contents of the source directory to the destination directory, and then deletes any files or directories that exist only in the destination, thus perfectly synchronizing the two locations. It is commonly used for backup synchronization, web server deployment, and development environment synchronization.
Key Features
- Perfect synchronization between source and destination directories
- Deletes files/directories present only in the destination
- Efficient synchronization through incremental transfer
- Supports remote synchronization over a network
Key Options
These are the main options used in conjunction with `rsync --delete`. The `--dry-run` option, in particular, should always be used first to prevent critical mistakes.
Synchronization Control
Generated command:
Try combining the commands.
Description:
`rsync-delete` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
`rsync --delete` is a powerful command, so it is crucial to always use the `--dry-run` option first to preview the results.
Preview Deletion Operations (Essential)
rsync -avn --delete /path/to/source/ /path/to/destination/
When synchronizing the source directory (`/path/to/source`) with the destination directory (`/path/to/destination`), preview which files will be deleted. No actual changes are made.
Actual Synchronization and Deletion
rsync -av --delete /path/to/source/ /path/to/destination/
After confirming safety with the preview, synchronize the source and destination and delete files that exist only in the destination. `-a` stands for archive mode, and `-v` for verbose output.
Synchronize and Delete, Excluding Specific Files/Directories
rsync -av --delete --exclude='logs/' --exclude='*.tmp' /path/to/source/ /path/to/destination/
Delete files in the destination that are not in the source, while excluding specific files or directories (e.g., the `logs/` directory and `*.tmp` files) from the deletion process.
Synchronize and Delete with a Remote Server
rsync -av --delete /path/to/local/source/ user@remote_host:/path/to/remote/destination/
Synchronize the contents of a local directory with a directory on a remote server, and delete files that exist only on the remote server. Transfer is securely done via SSH.
Tips & Precautions
`rsync --delete` can be a very powerful and destructive command, so it is essential to be aware of the following points when using it.
Most Important Precautions
- **Always use `--dry-run` (-n) first**: Always check which files will be deleted before executing the actual command.
- **Verify Source and Destination Paths**: Specifying incorrect paths can lead to unintended data loss.
- **Backup is Essential**: Always perform a backup before handling important data.
- **Importance of Trailing Slash (/)**: A trailing slash on the source path means copying the 'contents' of the directory, while no slash means copying the 'directory itself'. This distinction is important when using with `--delete`.
Difference in Path Trailing Slash (/)
The behavior changes depending on whether there is a trailing slash at the end of the source path.
- `rsync -av --delete source/ dest/`: Copies the *contents* of the `source` directory to `dest`, and deletes files that exist only in `dest`. The `source` directory itself is not created within `dest`.
- `rsync -av --delete source dest/`: Copies the `source` directory *itself* into the `dest` directory. This means the path `dest/source/` will be created. If `dest` already contains a file with the same name as `source`, it might be deleted.