Rsync Delete Files Not In Source

Article with TOC
Author's profile picture

Kalali

Jun 06, 2025 · 3 min read

Rsync Delete Files Not In Source
Rsync Delete Files Not In Source

Table of Contents

    Rsync Delete Files Not in Source: A Comprehensive Guide

    This article provides a comprehensive guide on how to use rsync to delete files on the destination that are not present in the source. This is a powerful feature, but requires careful consideration to avoid data loss. We'll cover the command, its implications, and best practices to ensure safe and effective use. Understanding this functionality allows for precise synchronization, keeping your destination directory a perfect mirror of your source.

    What is Rsync?

    Rsync (remote synchronization) is a powerful command-line utility used for efficiently transferring and synchronizing files and directories over a network. It's known for its speed, reliability, and ability to resume interrupted transfers. A key feature, often overlooked, is its capability to remove files from the destination that no longer exist in the source.

    The Crucial --delete Option

    The magic behind deleting files not in the source lies in the --delete option. When you add --delete to your rsync command, it instructs rsync to remove any files or directories on the destination that are not present in the source directory. This ensures a perfect one-to-one match between the source and destination.

    Basic Command Structure:

    The basic command structure for deleting files not in the source with rsync is:

    rsync -avz --delete source/ destination/
    
    • -a: archive mode. This recursively copies directories, preserving permissions, timestamps, and symbolic links.
    • -v: verbose mode. Shows detailed progress of the synchronization process.
    • -z: compression. Compresses data during transfer, useful for large files or slow connections.
    • --delete: This is the crucial option. It instructs rsync to delete files and directories on the destination that are not present in the source.
    • source/: the path to your source directory.
    • destination/: the path to your destination directory.

    Important Considerations and Best Practices:

    • Dry Run: Before executing the command with --delete, always perform a dry run using the --dry-run or -n option. This simulates the operation without making any actual changes. This is crucial to preview the changes rsync will make and avoid accidental data loss. This allows you to verify the intended deletions.
    rsync -avz --delete --dry-run source/ destination/
    
    • Backup: Before using --delete, it's strongly recommended to create a backup of your destination directory. This acts as a safety net in case something goes wrong.

    • Testing: Test the command thoroughly on a non-critical directory before applying it to important data.

    • Specific File Deletion: For more granular control, consider using --exclude to prevent accidental deletion of specific files or directories.

    • Understanding the Implications: Using --delete permanently removes files from your destination. There's no undo function. Carefully review the output of the dry run to ensure you are comfortable with the changes.

    • Permissions: Ensure you have the necessary permissions to delete files in the destination directory.

    Example Scenario:

    Let's say you have a source directory /home/user/source and a destination directory /home/user/destination. You want to synchronize them, deleting files on the destination that aren't in the source.

    First, perform a dry run:

    rsync -avz --delete --dry-run /home/user/source/ /home/user/destination/
    

    Review the output carefully. If it looks correct, proceed with the actual synchronization:

    rsync -avz --delete /home/user/source/ /home/user/destination/
    

    Conclusion:

    The --delete option in rsync is a powerful tool for maintaining precise synchronization between source and destination directories. However, its use requires caution and careful planning. Always perform a dry run and back up your data before using this option to prevent accidental data loss. By following these best practices, you can leverage the power of rsync to efficiently and safely manage your file synchronization. Remember to always prioritize data safety.

    Related Post

    Thank you for visiting our website which covers about Rsync Delete Files Not In Source . We hope the information provided has been useful to you. Feel free to contact us if you have any questions or need further assistance. See you next time and don't miss to bookmark.

    Go Home