Software & AppsOperating SystemLinux

How To Make Rsync Delete Files That Have Been Deleted from the Source Folder

Ubuntu 19

Rsync is a powerful tool used for copying and synchronizing files across different directories, either on the same machine or between two different machines. However, by default, rsync does not delete files from the destination directory that have been deleted from the source directory. This article will guide you on how to make rsync delete files that have been deleted from the source folder.

Quick Answer

To make rsync delete files that have been deleted from the source folder, you can use the --delete option in your rsync command. This option tells rsync to delete any files in the destination directory that do not exist in the source directory. Additionally, you can use the --delete-before or --delete-after options to specify when the deletions should occur.

Understanding Rsync

Rsync, which stands for “remote sync”, is a remote and local file synchronization tool. It uses an algorithm that minimizes data transfer by only moving the portions of files that have changed. This makes it a highly efficient tool, particularly for large files or directories.

The Need for Deletion in Rsync

There are instances when you want your destination directory to be an exact mirror of your source directory. This means that when files are deleted from the source directory, they should also be deleted from the destination directory. By default, rsync does not do this. However, it provides options that allow you to delete files from the destination directory.

Using the –delete Option

The --delete option in rsync allows you to delete files from the destination directory that do not exist in the source directory. Here’s an example of how to use it:

rsync -avh --delete source/ destination/

In this command:

  • -a stands for “archive”, which preserves permissions, ownerships, and timestamps
  • -v stands for “verbose”, which provides detailed output of the operation
  • -h stands for “human-readable”, which makes the output easier to understand
  • --delete tells rsync to delete files from the destination that are not in the source

Using the –delete-before and –delete-after Options

Rsync also provides options to specify when the deletions should occur. The --delete-before option tells rsync to delete files from the destination directory before transferring files from the source directory. The --delete-after option, on the other hand, tells rsync to delete files after the transfer. Here are examples of how to use these options:

rsync -avh --delete-before source/ destination/
rsync -avh --delete-after source/ destination/

Performing a Dry Run

Before running rsync with the --delete option, it’s recommended to perform a dry run using the -n or --dry-run option. This performs a trial run that doesn’t make any changes but shows what would be done. Here’s an example:

rsync -avh --delete --dry-run source/ destination/

Conclusion

Rsync is a powerful and flexible tool for synchronizing files and directories. With the --delete, --delete-before, and --delete-after options, you can ensure that your destination directory is an exact mirror of your source directory, including the deletion of files. Remember to always perform a dry run before making actual changes to avoid unintended deletions or modifications.

For more detailed information about rsync and its options, you can refer to the rsync man page.

Can I use rsync to delete files from the destination directory that have been deleted from the source directory?

Yes, you can use the --delete option in rsync to delete files from the destination directory that do not exist in the source directory.

How do I use the `–delete` option in rsync?

To use the --delete option, you can include it in your rsync command like this: rsync -avh --delete source/ destination/. This command will synchronize the source directory with the destination directory and delete any files in the destination directory that are not present in the source directory.

Leave a Comment

Your email address will not be published. Required fields are marked *