The rsync is the built-in synchronization tool considered for syncing the data from one location to another. Sometimes, the user may want to delete the destination synced files/directories, but the user wonders if doing so will keep the source file secure. The answer is the source directory will be secured. However, the rsync provides the “delete” flag to delete data from the destination.
This post will demonstrate the various examples of the rsync command to delete data in the destination directory.
- Delete Destination Files Through rsync Command
- Using “–delete” Flag
- Using “–delete-after” Flag
- Using “–delete-before” Flag
How to Delete Destination Files Through rsync Command?
There are various types of “delete” flags provided by the rsync command that users can consider, which are “delete,” “–delete-before,” and “–delete-after.” The syntax for using the rsync command is described below.
$ rsync [-Options] [Delete Option] [Source] [Destination]
- The “rsync” command to sync the files.
- The “Options” along with the rsync command.
- Utilize the “Delete Option” based on the requirement.
- Then enter the “Source” and “Destination.”
For the options to be used with rsync, run the help command in the terminal:
$ rsync --help
All the options for the rysnc command are listed.
Example 1: Delete all Files Using “–delete” Flag
The “delete” option of the rsync command will compare both source and destination locations before syncing. Then, delete all destination’s non-matching files. For instance, the directory “dir2” has the following content:
$ cd dir2 $ ls
The directory “dir2” has the files “file1.txt”, “file2.txt,” and “linux.txt.”
Let’s sync the files from the “dir1” to “dir2” to check the “delete” flag of the rsync command:
$ rsync -av --delete dir1/ dir2
The command is described as:
- The “rsync” command to sync the files.
- The “a” flag represents archive mode, and “v” for the verbose mode.
- The “delete” flag to delete the destination’s file.
- The “dir1/” is the source directory.
- The “dir2” is the destination directory.
The files on the destination are first deleted, then the source directory files are synced.
Verify the results by listing the content of the “dir2” directory:
$ ls dir2
The “command.txt” and “file.txt” is synced from the source directory.
Example 2: Delete Same Name of Files Using the “–delete-after” Flag
Likewise, the user can also examine the “–delete-after” flag of the rsync command, which copies the file first. Then compare both locations and delete non-matching files from the destination directory. Consider the following content in the “Remote” directory:
The “Remote” directory has three files “file.txt,” “file2.txt,” and “file3.txt”.
Let’s test the “–delete-after” flag in the following command:
$ rsync -av --delete-after dir1/ Remote
In the above command, the only change is the “–delete-after” flag to delete the files from the destination directory after copying the source files.
The source files “command.txt” and “file.txt” are copied first, then all non-matching files of the destination directory are deleted, as shown above.
Example 3: Delete Same Name of Files Using the “–delete-before” Flag
Comparably, the user can also use the “–delete-before” flag to delete files with the same names existing in the destination directory. The directory “dir1” and “Remote” has the following file content as shown:
$ ls dir1 $ ls Remote
Both directories “dir1” and “Remote” has the same file name, “file.txt”
To delete the matching files before copying the source files through rsync command is obtained as follows:
$ rsync -av --delete-before dir1/ Remote
The “–delete-before” flag to delete the same files before syncing the source files.
The file “command.txt” is synced to the “Remote” directory.
Note: If the same file name has the same context, that file will not be synced from the source. The rsync command considers that it is already present in the destination directory.
Verify the content of the “Remote” directory:
$ ls Remote
The files “command.txt” and “file.txt” are present in the directory.
The “rsync” command provides the options to delete the destination file, such as “–delete,” “–delete-after,” and “–delete-before.” The “delete” flag compares both locations. It deletes the non-matching files in the destination directory during syncing. The “–delete-after” flag syncs the files first and then deletes the non-matching files. While the “delete-before” compare both locations first and then delete the matching files in the destination directory.
This write-up briefly illustrated the deletion of the destination files while syncing using the rsync command in Linux.