Delete all folders containing files which match pattern

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








5















I'm trying to delete all subdirectories of my current working directory which contain a rar file.



My first attempt: find -name *.rar -exec rm -r /.. ';' failed because that is not a valid directory. I tried using dirname for a more sensible command, but decided to just ask after almost deleting stuff I didn't mean to.



I'm using Cygwin on Windows 7.










share|improve this question






























    5















    I'm trying to delete all subdirectories of my current working directory which contain a rar file.



    My first attempt: find -name *.rar -exec rm -r /.. ';' failed because that is not a valid directory. I tried using dirname for a more sensible command, but decided to just ask after almost deleting stuff I didn't mean to.



    I'm using Cygwin on Windows 7.










    share|improve this question


























      5












      5








      5


      4






      I'm trying to delete all subdirectories of my current working directory which contain a rar file.



      My first attempt: find -name *.rar -exec rm -r /.. ';' failed because that is not a valid directory. I tried using dirname for a more sensible command, but decided to just ask after almost deleting stuff I didn't mean to.



      I'm using Cygwin on Windows 7.










      share|improve this question
















      I'm trying to delete all subdirectories of my current working directory which contain a rar file.



      My first attempt: find -name *.rar -exec rm -r /.. ';' failed because that is not a valid directory. I tried using dirname for a more sensible command, but decided to just ask after almost deleting stuff I didn't mean to.



      I'm using Cygwin on Windows 7.







      find directory rm






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 9 at 14:19









      Rui F Ribeiro

      41.9k1483142




      41.9k1483142










      asked Dec 30 '12 at 21:55









      MikeFHayMikeFHay

      12815




      12815




















          5 Answers
          5






          active

          oldest

          votes


















          1














          You could do it with a pair of statements.



          First, get a list of directories to remove using



          find -name *.rar -exec dirname ';' > toremove


          Next, cat toremove to make sure it has the folders you want. Then, pass it to rm -rf using



          sed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r


          Last, rm toremove.






          share|improve this answer




















          • 1





            Excellent tip. This still doesn't quite work when the directory names contain spaces. I solved this by swapping the second line to sed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r

            – MikeFHay
            Dec 31 '12 at 1:27







          • 1





            This will not work on files with spaces. Also will break if the current directory contains a *.rar file due to lack of quoting.

            – jordanm
            Dec 31 '12 at 3:28












          • @MikeL: Thanks for the improvement; I replaced the second command with your version.

            – cpast
            Dec 31 '12 at 3:39






          • 2





            @MikeL This is good enough for you because Windows filenames can't contain " or newlines, but in general, this method is unsafe. There's no convenient way to use xargs (except xargs -0) with arbitrary file names. It's usually better to use find -exec.

            – Gilles
            Jan 1 '13 at 1:14


















          5














          With zsh:



          rm -rf **/*.rar(:h)


          The suffix :h applies the history expansion modifier h (“head”) which removes the basename of each match, keeping only the directory part.



          Make sure to check that these are really the directories you want to delete! For example, move them to a temporary directory first:



          mkdir DELETE
          mv **/*.rar(:h) DELETE/
          # check that you really want to delete everything in DELETE
          rm -r DELETE





          share|improve this answer






























            3














            You can use bash -c to perform more advanced operations in and -exec for find. The problem with using a temp file and cat in combination with xargs is that it will break if a file contains a space, newline, or tab. The following should work:



            find . -type f -name '*.rar' -exec bash -c 'rm -rf "$@%/*"' -- +


            Using + for find with "$@" will execute rm one time like with xargs.






            share|improve this answer
































              0














              Bash 4+ solution:



              shopt -s globstar
              for f in ./**/*.rar; do rm -rf "$f%/*"; done





              share|improve this answer






























                -1














                You could try something like this:



                for dir in */; do # iterate all subdirectories
                touch $dir"dummy.rar" # create a "rar"-file...
                for file in $dir*.rar; do # ...so this won't break on zero such
                rm $file # remove the dummy, and all archives
                done
                done





                share|improve this answer























                • OK, I read @cpast's answer and I see now that you want to remove the subdirectories, not the just the archives. My mistake! (But, isn't that a bit strange behavior?)

                  – Emanuel Berg
                  Jan 1 '13 at 1:32











                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%2f59890%2fdelete-all-folders-containing-files-which-match-pattern%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1














                You could do it with a pair of statements.



                First, get a list of directories to remove using



                find -name *.rar -exec dirname ';' > toremove


                Next, cat toremove to make sure it has the folders you want. Then, pass it to rm -rf using



                sed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r


                Last, rm toremove.






                share|improve this answer




















                • 1





                  Excellent tip. This still doesn't quite work when the directory names contain spaces. I solved this by swapping the second line to sed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r

                  – MikeFHay
                  Dec 31 '12 at 1:27







                • 1





                  This will not work on files with spaces. Also will break if the current directory contains a *.rar file due to lack of quoting.

                  – jordanm
                  Dec 31 '12 at 3:28












                • @MikeL: Thanks for the improvement; I replaced the second command with your version.

                  – cpast
                  Dec 31 '12 at 3:39






                • 2





                  @MikeL This is good enough for you because Windows filenames can't contain " or newlines, but in general, this method is unsafe. There's no convenient way to use xargs (except xargs -0) with arbitrary file names. It's usually better to use find -exec.

                  – Gilles
                  Jan 1 '13 at 1:14















                1














                You could do it with a pair of statements.



                First, get a list of directories to remove using



                find -name *.rar -exec dirname ';' > toremove


                Next, cat toremove to make sure it has the folders you want. Then, pass it to rm -rf using



                sed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r


                Last, rm toremove.






                share|improve this answer




















                • 1





                  Excellent tip. This still doesn't quite work when the directory names contain spaces. I solved this by swapping the second line to sed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r

                  – MikeFHay
                  Dec 31 '12 at 1:27







                • 1





                  This will not work on files with spaces. Also will break if the current directory contains a *.rar file due to lack of quoting.

                  – jordanm
                  Dec 31 '12 at 3:28












                • @MikeL: Thanks for the improvement; I replaced the second command with your version.

                  – cpast
                  Dec 31 '12 at 3:39






                • 2





                  @MikeL This is good enough for you because Windows filenames can't contain " or newlines, but in general, this method is unsafe. There's no convenient way to use xargs (except xargs -0) with arbitrary file names. It's usually better to use find -exec.

                  – Gilles
                  Jan 1 '13 at 1:14













                1












                1








                1







                You could do it with a pair of statements.



                First, get a list of directories to remove using



                find -name *.rar -exec dirname ';' > toremove


                Next, cat toremove to make sure it has the folders you want. Then, pass it to rm -rf using



                sed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r


                Last, rm toremove.






                share|improve this answer















                You could do it with a pair of statements.



                First, get a list of directories to remove using



                find -name *.rar -exec dirname ';' > toremove


                Next, cat toremove to make sure it has the folders you want. Then, pass it to rm -rf using



                sed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r


                Last, rm toremove.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 31 '12 at 3:38

























                answered Dec 31 '12 at 0:11









                cpastcpast

                27829




                27829







                • 1





                  Excellent tip. This still doesn't quite work when the directory names contain spaces. I solved this by swapping the second line to sed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r

                  – MikeFHay
                  Dec 31 '12 at 1:27







                • 1





                  This will not work on files with spaces. Also will break if the current directory contains a *.rar file due to lack of quoting.

                  – jordanm
                  Dec 31 '12 at 3:28












                • @MikeL: Thanks for the improvement; I replaced the second command with your version.

                  – cpast
                  Dec 31 '12 at 3:39






                • 2





                  @MikeL This is good enough for you because Windows filenames can't contain " or newlines, but in general, this method is unsafe. There's no convenient way to use xargs (except xargs -0) with arbitrary file names. It's usually better to use find -exec.

                  – Gilles
                  Jan 1 '13 at 1:14












                • 1





                  Excellent tip. This still doesn't quite work when the directory names contain spaces. I solved this by swapping the second line to sed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r

                  – MikeFHay
                  Dec 31 '12 at 1:27







                • 1





                  This will not work on files with spaces. Also will break if the current directory contains a *.rar file due to lack of quoting.

                  – jordanm
                  Dec 31 '12 at 3:28












                • @MikeL: Thanks for the improvement; I replaced the second command with your version.

                  – cpast
                  Dec 31 '12 at 3:39






                • 2





                  @MikeL This is good enough for you because Windows filenames can't contain " or newlines, but in general, this method is unsafe. There's no convenient way to use xargs (except xargs -0) with arbitrary file names. It's usually better to use find -exec.

                  – Gilles
                  Jan 1 '13 at 1:14







                1




                1





                Excellent tip. This still doesn't quite work when the directory names contain spaces. I solved this by swapping the second line to sed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r

                – MikeFHay
                Dec 31 '12 at 1:27






                Excellent tip. This still doesn't quite work when the directory names contain spaces. I solved this by swapping the second line to sed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r

                – MikeFHay
                Dec 31 '12 at 1:27





                1




                1





                This will not work on files with spaces. Also will break if the current directory contains a *.rar file due to lack of quoting.

                – jordanm
                Dec 31 '12 at 3:28






                This will not work on files with spaces. Also will break if the current directory contains a *.rar file due to lack of quoting.

                – jordanm
                Dec 31 '12 at 3:28














                @MikeL: Thanks for the improvement; I replaced the second command with your version.

                – cpast
                Dec 31 '12 at 3:39





                @MikeL: Thanks for the improvement; I replaced the second command with your version.

                – cpast
                Dec 31 '12 at 3:39




                2




                2





                @MikeL This is good enough for you because Windows filenames can't contain " or newlines, but in general, this method is unsafe. There's no convenient way to use xargs (except xargs -0) with arbitrary file names. It's usually better to use find -exec.

                – Gilles
                Jan 1 '13 at 1:14





                @MikeL This is good enough for you because Windows filenames can't contain " or newlines, but in general, this method is unsafe. There's no convenient way to use xargs (except xargs -0) with arbitrary file names. It's usually better to use find -exec.

                – Gilles
                Jan 1 '13 at 1:14













                5














                With zsh:



                rm -rf **/*.rar(:h)


                The suffix :h applies the history expansion modifier h (“head”) which removes the basename of each match, keeping only the directory part.



                Make sure to check that these are really the directories you want to delete! For example, move them to a temporary directory first:



                mkdir DELETE
                mv **/*.rar(:h) DELETE/
                # check that you really want to delete everything in DELETE
                rm -r DELETE





                share|improve this answer



























                  5














                  With zsh:



                  rm -rf **/*.rar(:h)


                  The suffix :h applies the history expansion modifier h (“head”) which removes the basename of each match, keeping only the directory part.



                  Make sure to check that these are really the directories you want to delete! For example, move them to a temporary directory first:



                  mkdir DELETE
                  mv **/*.rar(:h) DELETE/
                  # check that you really want to delete everything in DELETE
                  rm -r DELETE





                  share|improve this answer

























                    5












                    5








                    5







                    With zsh:



                    rm -rf **/*.rar(:h)


                    The suffix :h applies the history expansion modifier h (“head”) which removes the basename of each match, keeping only the directory part.



                    Make sure to check that these are really the directories you want to delete! For example, move them to a temporary directory first:



                    mkdir DELETE
                    mv **/*.rar(:h) DELETE/
                    # check that you really want to delete everything in DELETE
                    rm -r DELETE





                    share|improve this answer













                    With zsh:



                    rm -rf **/*.rar(:h)


                    The suffix :h applies the history expansion modifier h (“head”) which removes the basename of each match, keeping only the directory part.



                    Make sure to check that these are really the directories you want to delete! For example, move them to a temporary directory first:



                    mkdir DELETE
                    mv **/*.rar(:h) DELETE/
                    # check that you really want to delete everything in DELETE
                    rm -r DELETE






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 1 '13 at 1:12









                    GillesGilles

                    546k12911111624




                    546k12911111624





















                        3














                        You can use bash -c to perform more advanced operations in and -exec for find. The problem with using a temp file and cat in combination with xargs is that it will break if a file contains a space, newline, or tab. The following should work:



                        find . -type f -name '*.rar' -exec bash -c 'rm -rf "$@%/*"' -- +


                        Using + for find with "$@" will execute rm one time like with xargs.






                        share|improve this answer





























                          3














                          You can use bash -c to perform more advanced operations in and -exec for find. The problem with using a temp file and cat in combination with xargs is that it will break if a file contains a space, newline, or tab. The following should work:



                          find . -type f -name '*.rar' -exec bash -c 'rm -rf "$@%/*"' -- +


                          Using + for find with "$@" will execute rm one time like with xargs.






                          share|improve this answer



























                            3












                            3








                            3







                            You can use bash -c to perform more advanced operations in and -exec for find. The problem with using a temp file and cat in combination with xargs is that it will break if a file contains a space, newline, or tab. The following should work:



                            find . -type f -name '*.rar' -exec bash -c 'rm -rf "$@%/*"' -- +


                            Using + for find with "$@" will execute rm one time like with xargs.






                            share|improve this answer















                            You can use bash -c to perform more advanced operations in and -exec for find. The problem with using a temp file and cat in combination with xargs is that it will break if a file contains a space, newline, or tab. The following should work:



                            find . -type f -name '*.rar' -exec bash -c 'rm -rf "$@%/*"' -- +


                            Using + for find with "$@" will execute rm one time like with xargs.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 1 '13 at 20:33

























                            answered Dec 31 '12 at 3:31









                            jordanmjordanm

                            31.3k38897




                            31.3k38897





















                                0














                                Bash 4+ solution:



                                shopt -s globstar
                                for f in ./**/*.rar; do rm -rf "$f%/*"; done





                                share|improve this answer



























                                  0














                                  Bash 4+ solution:



                                  shopt -s globstar
                                  for f in ./**/*.rar; do rm -rf "$f%/*"; done





                                  share|improve this answer

























                                    0












                                    0








                                    0







                                    Bash 4+ solution:



                                    shopt -s globstar
                                    for f in ./**/*.rar; do rm -rf "$f%/*"; done





                                    share|improve this answer













                                    Bash 4+ solution:



                                    shopt -s globstar
                                    for f in ./**/*.rar; do rm -rf "$f%/*"; done






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered May 28 '13 at 20:52









                                    evilsoupevilsoup

                                    4,34421737




                                    4,34421737





















                                        -1














                                        You could try something like this:



                                        for dir in */; do # iterate all subdirectories
                                        touch $dir"dummy.rar" # create a "rar"-file...
                                        for file in $dir*.rar; do # ...so this won't break on zero such
                                        rm $file # remove the dummy, and all archives
                                        done
                                        done





                                        share|improve this answer























                                        • OK, I read @cpast's answer and I see now that you want to remove the subdirectories, not the just the archives. My mistake! (But, isn't that a bit strange behavior?)

                                          – Emanuel Berg
                                          Jan 1 '13 at 1:32















                                        -1














                                        You could try something like this:



                                        for dir in */; do # iterate all subdirectories
                                        touch $dir"dummy.rar" # create a "rar"-file...
                                        for file in $dir*.rar; do # ...so this won't break on zero such
                                        rm $file # remove the dummy, and all archives
                                        done
                                        done





                                        share|improve this answer























                                        • OK, I read @cpast's answer and I see now that you want to remove the subdirectories, not the just the archives. My mistake! (But, isn't that a bit strange behavior?)

                                          – Emanuel Berg
                                          Jan 1 '13 at 1:32













                                        -1












                                        -1








                                        -1







                                        You could try something like this:



                                        for dir in */; do # iterate all subdirectories
                                        touch $dir"dummy.rar" # create a "rar"-file...
                                        for file in $dir*.rar; do # ...so this won't break on zero such
                                        rm $file # remove the dummy, and all archives
                                        done
                                        done





                                        share|improve this answer













                                        You could try something like this:



                                        for dir in */; do # iterate all subdirectories
                                        touch $dir"dummy.rar" # create a "rar"-file...
                                        for file in $dir*.rar; do # ...so this won't break on zero such
                                        rm $file # remove the dummy, and all archives
                                        done
                                        done






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Dec 31 '12 at 3:20









                                        Emanuel BergEmanuel Berg

                                        3,75152954




                                        3,75152954












                                        • OK, I read @cpast's answer and I see now that you want to remove the subdirectories, not the just the archives. My mistake! (But, isn't that a bit strange behavior?)

                                          – Emanuel Berg
                                          Jan 1 '13 at 1:32

















                                        • OK, I read @cpast's answer and I see now that you want to remove the subdirectories, not the just the archives. My mistake! (But, isn't that a bit strange behavior?)

                                          – Emanuel Berg
                                          Jan 1 '13 at 1:32
















                                        OK, I read @cpast's answer and I see now that you want to remove the subdirectories, not the just the archives. My mistake! (But, isn't that a bit strange behavior?)

                                        – Emanuel Berg
                                        Jan 1 '13 at 1:32





                                        OK, I read @cpast's answer and I see now that you want to remove the subdirectories, not the just the archives. My mistake! (But, isn't that a bit strange behavior?)

                                        – Emanuel Berg
                                        Jan 1 '13 at 1:32

















                                        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%2f59890%2fdelete-all-folders-containing-files-which-match-pattern%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 check contact read email or not when send email to Individual?

                                        Displaying single band from multi-band raster using QGIS

                                        How many registers does an x86_64 CPU actually have?