32

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

user2028856
  • 1,261
  • 5
  • 15
  • 16

2 Answers2

37

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
heemayl
  • 94,145
4

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
A.B.
  • 92,275
  • What about it is working only partially? From what I can see it's behaving as described in the man page. Files not in A are being deleted from B. The man page doesn't make any mention of update times. – nicholsonjf Feb 08 '23 at 21:13
  • 1
    @nicholsonjf, if I correctly understood what I'd read elsewhere, --ignore-existing has an odd (and not stated in manual) behaviour of still changing the modified date of the existing file @ the target location, even though it doesn't actually replace it. To actually not modify the target location's file in any way whatsoever, one must use the --update or -u flag. – Shaun of the Dead Feb 16 '23 at 21:42
  • @ShaunoftheDead "one must use the --update or -u flag." how? Please explain in full? give an example? – Tom Feb 19 '23 at 12:07
  • 1
    Sure, as I understand it, rsync -u [source] [destination] will leave already-existing files unchanged (including their dates of being modified) but will add ones which are missing in the [destination]. – Shaun of the Dead Feb 24 '23 at 13:43