Rsynchronizing or syncing your files using Rsync is a simple but crucial task, and including deleted files is a significant aspect. Rsync is a fast, extraordinarily versatile, and powerful tool for syncing files in Unix and Linux based systems. Typically, when rsync is used, it checks for the differences between files in source and destination and transmits only the changed bits of files, reducing data transfer volumes.
The —delete option is needed to sync deleted files. This operation tells rsync to delete extraneous files from the receiving side or the destination folder. Essentially, this ensures that whatever deletion operation has occurred on the source directory also reflects on the destination directory.
Let’s consider a simple example; we have a directory named Folder01 containing files A, B, and C in Source location and already synchronized in the Destination. We decide we no longer need file B. When we delete file B from Source and automatically update that deletion on the Destination, we use:
```
rsync -av —delete /source/directory /destination/directory
```
The -a option in rsync is the “archive mode.” It syncs directories recursively and secures links and file permissions, while the -v option stands for “verbose,” which tells rsync to describe the process in detail. However, you should consider using the —delete command carefully as it might remove files unexpectedly, if you’re unsure of what’s on the source directory.
Remember to run the dry-run (-n option) before proceeding with your actual rsync operation, especially in production environment, to preview the sync operations. A dry run will perform a trial run that doesn’t make any changes and can predict the effects of the rsync command. It should look like this:
```
rsync -av —delete -n /source/directory /destination/directory
```
The —delete option in rsync has few more options:
- —delete-before: Receiver will delete before transferring.
- —delete-during: Receiver will delete while transferring.
- —delete-delay: Receiver will delete after the transfer, but before the directory is deleted.
- —delete-after: Receiver will delete but after entire transfer has been completed.
For this information, the main source used was the rsync man page: “https://linux.die.net/man/1/rsync.” It is an extensive manual about rsync, discussed in layman’s terms. It contains a list of options and their meanings, with examples. The other significant source was a Stack Overflow thread: “https://stackoverflow.com/questions/20244585/how-do-i-use-rsync-to-sync-removing-deleted-files,” where a similar question was asked and answered in context. This effectively illustrates the application of how to sync deleted files with rsync.