rsync delete files from target if not existing in source

218

Solution 1

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

Solution 2

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
Share:
218

Related videos on Youtube

Muhammad Abdullah
Author by

Muhammad Abdullah

Updated on September 18, 2022

Comments

  • Muhammad Abdullah
    Muhammad Abdullah over 1 year

    I am writing instrumentation test cases of a method that checks the state of my android device. This is how im mocking the context:

    mContext = InstrumentationRegistry.getInstrumentation().getContext();
    

    I am mocking the context for this statement:

     BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
    

    But upon reaching this statement it gives the following exception:

     ----- begin exception -----
    E/TestRunner: java.lang.IllegalArgumentException: context not associated with any application (using a mock context?)
            at android.bluetooth.BluetoothManager.<init>(BluetoothManager.java:66)
            at android.app.SystemServiceRegistry$8.createService(SystemServiceRegistry.java:242)
            at android.app.SystemServiceRegistry$8.createService(SystemServiceRegistry.java:240)
            at android.app.SystemServiceRegistry$CachedServiceFetcher.getService(SystemServiceRegistry.java:997)
            at android.app.SystemServiceRegistry.getSystemService(SystemServiceRegistry.java:949)
            at android.app.ContextImpl.getSystemService(ContextImpl.java:1847)
            at com.resatech.android.scoutlib.uidata.ScoutDevice.init(ScoutDevice.java:251)
            at com.resatech.android.scoutlib.uidata.ScoutDevice.<init>(ScoutDevice.java:190)
            at com.resatech.android.scoutandroid.viewModels.ScoutDeviceSystemStatsViewModelTest.ScoutDeviceSystemStatsViewModelTest_ValuesUnknown(ScoutDeviceSystemStatsViewModelTest.java:74)
            at java.lang.reflect.Method.invoke(Native Method)
            at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
            at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
            at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
            at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
            at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
            at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
            at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
            at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
            at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
            at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
            at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
            at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
            at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
            at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
            at org.junit.runners.Suite.runChild(Suite.java:128)
            at org.junit.runners.Suite.runChild(Suite.java:27)
            at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
            at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
            at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
            at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
            at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
            at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
            at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
            at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
            at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
            at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
            at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2119)
        ----- end exception -----
    

    Can someone help me and tell me if im mocking it incorrectly or what is the issue? Any guidance would be highly appreciated ?