rsync delete files from target if not existing in source
Okay so I have looked up the existing answers here and elsewhere but what I can't find out is, if I use the --ignore-existing option along with the --delete option, will this combination I still be able to have rsync delete files from the target if they no longer exist in the source AND still prevent rsync from overwriting existing files in the target?
Thanks
2 Answers
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
123Now 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