Delete files that exist in the source with rsync












1















I have a folder on server "A" called ./delete.



I want to delete everything in the folder ./stuff on server "B" that exists in the ./delete folder on server "A".



Its like the reverse of rsync. instead of transferring files from a:/delete to b:/stuff I want to just delete all the files that are in both places on server "B".



like a --delete-on-destination flag or something.










share|improve this question





























    1















    I have a folder on server "A" called ./delete.



    I want to delete everything in the folder ./stuff on server "B" that exists in the ./delete folder on server "A".



    Its like the reverse of rsync. instead of transferring files from a:/delete to b:/stuff I want to just delete all the files that are in both places on server "B".



    like a --delete-on-destination flag or something.










    share|improve this question



























      1












      1








      1


      1






      I have a folder on server "A" called ./delete.



      I want to delete everything in the folder ./stuff on server "B" that exists in the ./delete folder on server "A".



      Its like the reverse of rsync. instead of transferring files from a:/delete to b:/stuff I want to just delete all the files that are in both places on server "B".



      like a --delete-on-destination flag or something.










      share|improve this question
















      I have a folder on server "A" called ./delete.



      I want to delete everything in the folder ./stuff on server "B" that exists in the ./delete folder on server "A".



      Its like the reverse of rsync. instead of transferring files from a:/delete to b:/stuff I want to just delete all the files that are in both places on server "B".



      like a --delete-on-destination flag or something.







      rsync rm






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 21 '13 at 22:24









      Gilles

      543k12811001617




      543k12811001617










      asked Aug 21 '13 at 3:13









      chovychovy

      5493616




      5493616






















          3 Answers
          3






          active

          oldest

          votes


















          -1














          I don't think that rsync can resolve your problem. But another sync tool might be able to help you. Take a look at unison.



          Unison has many features and can sync two folders on different machines. Unison is written in OCaml that is a charming functionional language.



          Rsync is a unidirectional sync tool.Unison is a bidirectional sync tool.






          share|improve this answer


























          • thanks. i used a series of scripts to accomplish what I wanted, but this might work.

            – chovy
            Aug 21 '13 at 7:46











          • How would Unison help here? The purpose isn't to synchronize the two directories at all.

            – Gilles
            Aug 21 '13 at 22:26











          • after looking at unison, I figure it was just a blanket answer without really reading the problem I am faced with.

            – chovy
            Aug 22 '13 at 5:33











          • Unison can keep the two folder on different machine sync.If clientA delete some files,clientB will delete some files too!

            – Edward Shen
            Aug 23 '13 at 1:49



















          0














          File copy tools such as rsync aren't going to help you easily since you don't want to copy any files.



          The straightforward approach of listing the files on server A and erasing these files on server B is a good one in most circumstances. It's easier to cope with arbitrary file names if your servers' find and xargs commands understand null separators (Linux, *BSD, Cygwin). From A:



          cd ./delete
          find . ! -type d -print0 | ssh B 'cd /path/to/stuff && xargs -0 rm -f'


          This may leave some empty directories behind. You can delete all empty directories (even the ones that were empty before) with



          ssh B 'cd /path/to/stuff && find . -depth -type d -exec rmdir {} + 2>/dev/null'


          If you only want to remove directories that existed on the source side, you'll need to use the list again:



          find . -depth -type d -print0  | ssh B 'cd /path/to/stuff && xargs -0 rmdir'


          If there are directory trees on A that contain a lot of files and don't exist on B, this transfers the whole list of files to erase where a well-chosen rm -rf on B would do the same work locally on B but would save a lot of bandwidth in the transfer. This is the kind of situation where a file synchronization tool would fare well. You could run rsync -nv and try to parse the output, but it isn't easy to build something reliable on this.






          share|improve this answer


























          • this only works on files. not directories, and I believe it deletes the files from A, not B.

            – chovy
            Aug 22 '13 at 5:32











          • @chovy Indeed, I'd misread your question. I don't think file synchronization tools will help much since you aren't copying files. Although, hmmm, there might be a way to build something with rsync --remove-source-files --compare-dest

            – Gilles
            Aug 22 '13 at 23:33











          • i don't understand what --compare-dest does.

            – chovy
            Aug 24 '13 at 21:13



















          0














          I wanted to do this also, and decided to just write a ruby script for it.



          Here you go:



          https://github.com/saizai/utils/blob/master/rm_dupes_from.rb






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f87553%2fdelete-files-that-exist-in-the-source-with-rsync%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            -1














            I don't think that rsync can resolve your problem. But another sync tool might be able to help you. Take a look at unison.



            Unison has many features and can sync two folders on different machines. Unison is written in OCaml that is a charming functionional language.



            Rsync is a unidirectional sync tool.Unison is a bidirectional sync tool.






            share|improve this answer


























            • thanks. i used a series of scripts to accomplish what I wanted, but this might work.

              – chovy
              Aug 21 '13 at 7:46











            • How would Unison help here? The purpose isn't to synchronize the two directories at all.

              – Gilles
              Aug 21 '13 at 22:26











            • after looking at unison, I figure it was just a blanket answer without really reading the problem I am faced with.

              – chovy
              Aug 22 '13 at 5:33











            • Unison can keep the two folder on different machine sync.If clientA delete some files,clientB will delete some files too!

              – Edward Shen
              Aug 23 '13 at 1:49
















            -1














            I don't think that rsync can resolve your problem. But another sync tool might be able to help you. Take a look at unison.



            Unison has many features and can sync two folders on different machines. Unison is written in OCaml that is a charming functionional language.



            Rsync is a unidirectional sync tool.Unison is a bidirectional sync tool.






            share|improve this answer


























            • thanks. i used a series of scripts to accomplish what I wanted, but this might work.

              – chovy
              Aug 21 '13 at 7:46











            • How would Unison help here? The purpose isn't to synchronize the two directories at all.

              – Gilles
              Aug 21 '13 at 22:26











            • after looking at unison, I figure it was just a blanket answer without really reading the problem I am faced with.

              – chovy
              Aug 22 '13 at 5:33











            • Unison can keep the two folder on different machine sync.If clientA delete some files,clientB will delete some files too!

              – Edward Shen
              Aug 23 '13 at 1:49














            -1












            -1








            -1







            I don't think that rsync can resolve your problem. But another sync tool might be able to help you. Take a look at unison.



            Unison has many features and can sync two folders on different machines. Unison is written in OCaml that is a charming functionional language.



            Rsync is a unidirectional sync tool.Unison is a bidirectional sync tool.






            share|improve this answer















            I don't think that rsync can resolve your problem. But another sync tool might be able to help you. Take a look at unison.



            Unison has many features and can sync two folders on different machines. Unison is written in OCaml that is a charming functionional language.



            Rsync is a unidirectional sync tool.Unison is a bidirectional sync tool.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 21 '13 at 9:34

























            answered Aug 21 '13 at 6:13









            Edward ShenEdward Shen

            67237




            67237













            • thanks. i used a series of scripts to accomplish what I wanted, but this might work.

              – chovy
              Aug 21 '13 at 7:46











            • How would Unison help here? The purpose isn't to synchronize the two directories at all.

              – Gilles
              Aug 21 '13 at 22:26











            • after looking at unison, I figure it was just a blanket answer without really reading the problem I am faced with.

              – chovy
              Aug 22 '13 at 5:33











            • Unison can keep the two folder on different machine sync.If clientA delete some files,clientB will delete some files too!

              – Edward Shen
              Aug 23 '13 at 1:49



















            • thanks. i used a series of scripts to accomplish what I wanted, but this might work.

              – chovy
              Aug 21 '13 at 7:46











            • How would Unison help here? The purpose isn't to synchronize the two directories at all.

              – Gilles
              Aug 21 '13 at 22:26











            • after looking at unison, I figure it was just a blanket answer without really reading the problem I am faced with.

              – chovy
              Aug 22 '13 at 5:33











            • Unison can keep the two folder on different machine sync.If clientA delete some files,clientB will delete some files too!

              – Edward Shen
              Aug 23 '13 at 1:49

















            thanks. i used a series of scripts to accomplish what I wanted, but this might work.

            – chovy
            Aug 21 '13 at 7:46





            thanks. i used a series of scripts to accomplish what I wanted, but this might work.

            – chovy
            Aug 21 '13 at 7:46













            How would Unison help here? The purpose isn't to synchronize the two directories at all.

            – Gilles
            Aug 21 '13 at 22:26





            How would Unison help here? The purpose isn't to synchronize the two directories at all.

            – Gilles
            Aug 21 '13 at 22:26













            after looking at unison, I figure it was just a blanket answer without really reading the problem I am faced with.

            – chovy
            Aug 22 '13 at 5:33





            after looking at unison, I figure it was just a blanket answer without really reading the problem I am faced with.

            – chovy
            Aug 22 '13 at 5:33













            Unison can keep the two folder on different machine sync.If clientA delete some files,clientB will delete some files too!

            – Edward Shen
            Aug 23 '13 at 1:49





            Unison can keep the two folder on different machine sync.If clientA delete some files,clientB will delete some files too!

            – Edward Shen
            Aug 23 '13 at 1:49













            0














            File copy tools such as rsync aren't going to help you easily since you don't want to copy any files.



            The straightforward approach of listing the files on server A and erasing these files on server B is a good one in most circumstances. It's easier to cope with arbitrary file names if your servers' find and xargs commands understand null separators (Linux, *BSD, Cygwin). From A:



            cd ./delete
            find . ! -type d -print0 | ssh B 'cd /path/to/stuff && xargs -0 rm -f'


            This may leave some empty directories behind. You can delete all empty directories (even the ones that were empty before) with



            ssh B 'cd /path/to/stuff && find . -depth -type d -exec rmdir {} + 2>/dev/null'


            If you only want to remove directories that existed on the source side, you'll need to use the list again:



            find . -depth -type d -print0  | ssh B 'cd /path/to/stuff && xargs -0 rmdir'


            If there are directory trees on A that contain a lot of files and don't exist on B, this transfers the whole list of files to erase where a well-chosen rm -rf on B would do the same work locally on B but would save a lot of bandwidth in the transfer. This is the kind of situation where a file synchronization tool would fare well. You could run rsync -nv and try to parse the output, but it isn't easy to build something reliable on this.






            share|improve this answer


























            • this only works on files. not directories, and I believe it deletes the files from A, not B.

              – chovy
              Aug 22 '13 at 5:32











            • @chovy Indeed, I'd misread your question. I don't think file synchronization tools will help much since you aren't copying files. Although, hmmm, there might be a way to build something with rsync --remove-source-files --compare-dest

              – Gilles
              Aug 22 '13 at 23:33











            • i don't understand what --compare-dest does.

              – chovy
              Aug 24 '13 at 21:13
















            0














            File copy tools such as rsync aren't going to help you easily since you don't want to copy any files.



            The straightforward approach of listing the files on server A and erasing these files on server B is a good one in most circumstances. It's easier to cope with arbitrary file names if your servers' find and xargs commands understand null separators (Linux, *BSD, Cygwin). From A:



            cd ./delete
            find . ! -type d -print0 | ssh B 'cd /path/to/stuff && xargs -0 rm -f'


            This may leave some empty directories behind. You can delete all empty directories (even the ones that were empty before) with



            ssh B 'cd /path/to/stuff && find . -depth -type d -exec rmdir {} + 2>/dev/null'


            If you only want to remove directories that existed on the source side, you'll need to use the list again:



            find . -depth -type d -print0  | ssh B 'cd /path/to/stuff && xargs -0 rmdir'


            If there are directory trees on A that contain a lot of files and don't exist on B, this transfers the whole list of files to erase where a well-chosen rm -rf on B would do the same work locally on B but would save a lot of bandwidth in the transfer. This is the kind of situation where a file synchronization tool would fare well. You could run rsync -nv and try to parse the output, but it isn't easy to build something reliable on this.






            share|improve this answer


























            • this only works on files. not directories, and I believe it deletes the files from A, not B.

              – chovy
              Aug 22 '13 at 5:32











            • @chovy Indeed, I'd misread your question. I don't think file synchronization tools will help much since you aren't copying files. Although, hmmm, there might be a way to build something with rsync --remove-source-files --compare-dest

              – Gilles
              Aug 22 '13 at 23:33











            • i don't understand what --compare-dest does.

              – chovy
              Aug 24 '13 at 21:13














            0












            0








            0







            File copy tools such as rsync aren't going to help you easily since you don't want to copy any files.



            The straightforward approach of listing the files on server A and erasing these files on server B is a good one in most circumstances. It's easier to cope with arbitrary file names if your servers' find and xargs commands understand null separators (Linux, *BSD, Cygwin). From A:



            cd ./delete
            find . ! -type d -print0 | ssh B 'cd /path/to/stuff && xargs -0 rm -f'


            This may leave some empty directories behind. You can delete all empty directories (even the ones that were empty before) with



            ssh B 'cd /path/to/stuff && find . -depth -type d -exec rmdir {} + 2>/dev/null'


            If you only want to remove directories that existed on the source side, you'll need to use the list again:



            find . -depth -type d -print0  | ssh B 'cd /path/to/stuff && xargs -0 rmdir'


            If there are directory trees on A that contain a lot of files and don't exist on B, this transfers the whole list of files to erase where a well-chosen rm -rf on B would do the same work locally on B but would save a lot of bandwidth in the transfer. This is the kind of situation where a file synchronization tool would fare well. You could run rsync -nv and try to parse the output, but it isn't easy to build something reliable on this.






            share|improve this answer















            File copy tools such as rsync aren't going to help you easily since you don't want to copy any files.



            The straightforward approach of listing the files on server A and erasing these files on server B is a good one in most circumstances. It's easier to cope with arbitrary file names if your servers' find and xargs commands understand null separators (Linux, *BSD, Cygwin). From A:



            cd ./delete
            find . ! -type d -print0 | ssh B 'cd /path/to/stuff && xargs -0 rm -f'


            This may leave some empty directories behind. You can delete all empty directories (even the ones that were empty before) with



            ssh B 'cd /path/to/stuff && find . -depth -type d -exec rmdir {} + 2>/dev/null'


            If you only want to remove directories that existed on the source side, you'll need to use the list again:



            find . -depth -type d -print0  | ssh B 'cd /path/to/stuff && xargs -0 rmdir'


            If there are directory trees on A that contain a lot of files and don't exist on B, this transfers the whole list of files to erase where a well-chosen rm -rf on B would do the same work locally on B but would save a lot of bandwidth in the transfer. This is the kind of situation where a file synchronization tool would fare well. You could run rsync -nv and try to parse the output, but it isn't easy to build something reliable on this.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 22 '13 at 23:32

























            answered Aug 21 '13 at 22:27









            GillesGilles

            543k12811001617




            543k12811001617













            • this only works on files. not directories, and I believe it deletes the files from A, not B.

              – chovy
              Aug 22 '13 at 5:32











            • @chovy Indeed, I'd misread your question. I don't think file synchronization tools will help much since you aren't copying files. Although, hmmm, there might be a way to build something with rsync --remove-source-files --compare-dest

              – Gilles
              Aug 22 '13 at 23:33











            • i don't understand what --compare-dest does.

              – chovy
              Aug 24 '13 at 21:13



















            • this only works on files. not directories, and I believe it deletes the files from A, not B.

              – chovy
              Aug 22 '13 at 5:32











            • @chovy Indeed, I'd misread your question. I don't think file synchronization tools will help much since you aren't copying files. Although, hmmm, there might be a way to build something with rsync --remove-source-files --compare-dest

              – Gilles
              Aug 22 '13 at 23:33











            • i don't understand what --compare-dest does.

              – chovy
              Aug 24 '13 at 21:13

















            this only works on files. not directories, and I believe it deletes the files from A, not B.

            – chovy
            Aug 22 '13 at 5:32





            this only works on files. not directories, and I believe it deletes the files from A, not B.

            – chovy
            Aug 22 '13 at 5:32













            @chovy Indeed, I'd misread your question. I don't think file synchronization tools will help much since you aren't copying files. Although, hmmm, there might be a way to build something with rsync --remove-source-files --compare-dest

            – Gilles
            Aug 22 '13 at 23:33





            @chovy Indeed, I'd misread your question. I don't think file synchronization tools will help much since you aren't copying files. Although, hmmm, there might be a way to build something with rsync --remove-source-files --compare-dest

            – Gilles
            Aug 22 '13 at 23:33













            i don't understand what --compare-dest does.

            – chovy
            Aug 24 '13 at 21:13





            i don't understand what --compare-dest does.

            – chovy
            Aug 24 '13 at 21:13











            0














            I wanted to do this also, and decided to just write a ruby script for it.



            Here you go:



            https://github.com/saizai/utils/blob/master/rm_dupes_from.rb






            share|improve this answer




























              0














              I wanted to do this also, and decided to just write a ruby script for it.



              Here you go:



              https://github.com/saizai/utils/blob/master/rm_dupes_from.rb






              share|improve this answer


























                0












                0








                0







                I wanted to do this also, and decided to just write a ruby script for it.



                Here you go:



                https://github.com/saizai/utils/blob/master/rm_dupes_from.rb






                share|improve this answer













                I wanted to do this also, and decided to just write a ruby script for it.



                Here you go:



                https://github.com/saizai/utils/blob/master/rm_dupes_from.rb







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 27 at 15:09









                SaiSai

                1011




                1011






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f87553%2fdelete-files-that-exist-in-the-source-with-rsync%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

                    is 'sed' thread safe

                    How to make a Squid Proxy server?