rsync delete files from target if not existing in source
Yes, --delete
and --ignore-existing
options of rsync
will work together.
Here is a test (check the modification times especially):
:~/foo$ ls -l
-rw-rw-r-- 1 user user 0 Mar 27 14:38 egg.txt
-rw-rw-r-- 1 user user 0 Mar 27 14:38 ignore.txt
-rw-rw-r-- 1 user user 0 Mar 27 14:38 spam.txt
:~/bar$ ls -l
-rw-rw-r-- 1 user user 0 Mar 27 14:40 ignore.txt
-rw-rw-r-- 1 user user 0 Mar 27 14:40 remove.txt
$ rsync -r --delete --ignore-existing ~/foo/ ~/bar/
:~/bar$ ls -l
-rw-rw-r-- 1 user user 0 Mar 27 14:42 egg.txt
-rw-rw-r-- 1 user user 0 Mar 27 14:40 ignore.txt
-rw-rw-r-- 1 user user 0 Mar 27 14:42 spam.txt
This works only partially
From rsync man page:
[..]
--delete delete extraneous files from dest dirs
[..]
--ignore-existing skip updating files that exist on receiver
[..]
Example:
% ls ~/tmp/A
123 456
% ls ~/tmp/B
456
% rsync --recursive --ignore-existing --delete ~/tmp/A/ ~/tmp/B
% ls ~/tmp/B
123 456
% rm ~/tmp/A/456
% rsync --recursive --ignore-existing --delete ~/tmp/A/ ~/tmp/B
% ls ~/tmp/B
123
% touch ~/tmp/B/789
% ls ~/tmp/B
123 789
% rsync --recursive --ignore-existing --delete ~/tmp/A/ ~/tmp/B
% ls ~/tmp/B
123
Now pay attention to the change date of 456
% ls -la ~/tmp/A
insgesamt 8
drwxrwxr-x 2 aboettger aboettger 4096 Mär 27 09:41 .
drwxrwxr-x 7 aboettger aboettger 4096 Mär 27 09:25 ..
-rw-rw-r-- 1 aboettger aboettger 0 Mär 27 09:35 123
-rw-rw-r-- 1 aboettger aboettger 0 Mär 27 09:42 456
% rsync --recursive --ignore-existing --delete ~/tmp/A/ ~/tmp/B
% ls -la ~/tmp/B
insgesamt 8
drwxrwxr-x 2 aboettger aboettger 4096 Mär 27 09:45 .
drwxrwxr-x 7 aboettger aboettger 4096 Mär 27 09:25 ..
-rw-rw-r-- 1 aboettger aboettger 0 Mär 27 09:35 123
-rw-rw-r-- 1 aboettger aboettger 0 Mär 27 09:42 456
% touch ~/tmp/A/456
% ls -la ~/tmp/A
insgesamt 8
drwxrwxr-x 2 aboettger aboettger 4096 Mär 27 09:41 .
drwxrwxr-x 7 aboettger aboettger 4096 Mär 27 09:25 ..
-rw-rw-r-- 1 aboettger aboettger 0 Mär 27 09:35 123
-rw-rw-r-- 1 aboettger aboettger 0 Mär 27 09:46 456
% rsync --recursive --ignore-existing --delete ~/tmp/A/ ~/tmp/B
% ls -la ~/tmp/B
insgesamt 8
drwxrwxr-x 2 aboettger aboettger 4096 Mär 27 09:45 .
drwxrwxr-x 7 aboettger aboettger 4096 Mär 27 09:25 ..
-rw-rw-r-- 1 aboettger aboettger 0 Mär 27 09:35 123
-rw-rw-r-- 1 aboettger aboettger 0 Mär 27 09:42 456