I am trying to do an rysnc which would cause files in the destination directory that are not in the source directory to be deleted. As a result there would be the same number of files in the source and destination directories. Based on google searches and other stack overflow inquiries I tried the following command:
rsync -avz -e -d /home/web/dataprocess/testwind/*.dbf -d /home/web/newcheck/ --delete
While this did rsync over the files from the source directory that were dbf files, it did not delete the extraneous *.dbf files that are not present in the source directory like people said it would. How can I get this to delete the extra *dbf file in the destination directory that is not in the source directory?
The reason your command doesn't work is stated in the rsync
man page:
--delete
This tells rsync to delete extraneous files from the receiving side
(ones that aren't on the sending side), but only for the directories
that are being synchronized. You must have asked rsync to send the whole
directory (e.g. dir or dir/) without using a wildcard for the
directory's contents (e.g. dir/*) since the wildcard is expanded by
the shell and rsync thus gets a request to transfer individual files,
not the files' parent directory. Files that are excluded from the
transfer are also excluded from being deleted unless you use the
--delete-excluded option or mark the rules as only matching on the
sending side (see the include/exclude modifiers in the FILTER RULES
section).
However, using rsync filters to protect non-dbf files from deletion, we can use this command to accomplish your task:
rsync -avz -e -d src/ -d dest/ --delete --filter="R *.dbf" --filter="P **"
This should work and also properly creates subdirectories copying .dbfs (and deleting .dbfs without matches.)
rsync -avz -e -d /home/web/dataprocess/testwind -d /home/web/newcheck/ --delete --filter="+ */" --filter="+ *.dbf" --filter="- *"
Each file is passed through the filters in order, so the filters mean "Keep subdirectories, keep *.dbfs, and ignore everything else." (If you change the order of the filter arguments the result is probably incorrect. It's also not possible to pass multiple filters using one --filter, hence why the command looks a little redundant.)
When running this, you'll occasionally see a warning message "Cannot delete non-empty directory: some directory" if there is a directory in the destination that is not on the source and that directory contains at least one file that is not a .dbf. This warning can be safely ignored.