delete folders with the same name as the parent folders recursively in bash?











up vote
0
down vote

favorite












So some file copying went wrong in one of my machines and I'm wasting a ludicrous amount of space by having a copy of each folder inside of the parent with the same name, and i'd want to delete them all. Example:



 /mnt/test/files/foo
_ /mnt/test/files/foo/file1 (etc)
|__ /mnt/test/files/foo/foo
_ /mnt/test/files/foo/foo/file1 (etc)
|_ /mnt/files/foo/foo2
_ /mnt/files/foo/foo2/file1 (etc)
|_ /mnt/files/foo/foo2/foo2
_ /mnt/files/foo/foo2/foo2/file1 (etc)


so obviously i want to delete /mnt/files/foo/foo , /mnt/files/foo/foo2/foo2 and its contents (and so on) entirely and stop wasting space. What would be a good way to script it in bash?



Thanks in advance.










share|improve this question







New contributor




Webodan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite












    So some file copying went wrong in one of my machines and I'm wasting a ludicrous amount of space by having a copy of each folder inside of the parent with the same name, and i'd want to delete them all. Example:



     /mnt/test/files/foo
    _ /mnt/test/files/foo/file1 (etc)
    |__ /mnt/test/files/foo/foo
    _ /mnt/test/files/foo/foo/file1 (etc)
    |_ /mnt/files/foo/foo2
    _ /mnt/files/foo/foo2/file1 (etc)
    |_ /mnt/files/foo/foo2/foo2
    _ /mnt/files/foo/foo2/foo2/file1 (etc)


    so obviously i want to delete /mnt/files/foo/foo , /mnt/files/foo/foo2/foo2 and its contents (and so on) entirely and stop wasting space. What would be a good way to script it in bash?



    Thanks in advance.










    share|improve this question







    New contributor




    Webodan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      So some file copying went wrong in one of my machines and I'm wasting a ludicrous amount of space by having a copy of each folder inside of the parent with the same name, and i'd want to delete them all. Example:



       /mnt/test/files/foo
      _ /mnt/test/files/foo/file1 (etc)
      |__ /mnt/test/files/foo/foo
      _ /mnt/test/files/foo/foo/file1 (etc)
      |_ /mnt/files/foo/foo2
      _ /mnt/files/foo/foo2/file1 (etc)
      |_ /mnt/files/foo/foo2/foo2
      _ /mnt/files/foo/foo2/foo2/file1 (etc)


      so obviously i want to delete /mnt/files/foo/foo , /mnt/files/foo/foo2/foo2 and its contents (and so on) entirely and stop wasting space. What would be a good way to script it in bash?



      Thanks in advance.










      share|improve this question







      New contributor




      Webodan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      So some file copying went wrong in one of my machines and I'm wasting a ludicrous amount of space by having a copy of each folder inside of the parent with the same name, and i'd want to delete them all. Example:



       /mnt/test/files/foo
      _ /mnt/test/files/foo/file1 (etc)
      |__ /mnt/test/files/foo/foo
      _ /mnt/test/files/foo/foo/file1 (etc)
      |_ /mnt/files/foo/foo2
      _ /mnt/files/foo/foo2/file1 (etc)
      |_ /mnt/files/foo/foo2/foo2
      _ /mnt/files/foo/foo2/foo2/file1 (etc)


      so obviously i want to delete /mnt/files/foo/foo , /mnt/files/foo/foo2/foo2 and its contents (and so on) entirely and stop wasting space. What would be a good way to script it in bash?



      Thanks in advance.







      linux bash scripting terminal






      share|improve this question







      New contributor




      Webodan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Webodan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Webodan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 days ago









      Webodan

      31




      31




      New contributor




      Webodan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Webodan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Webodan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          If your find supports the -regex predicate, you can list the directories with:



          find . -type d -regex '.*/([^/]*)/1' -prune -print


          to remove them, you can change -print to:



          -exec rm -rf {} +


          But be sure to check the list first so you don't delete any files you need.






          share|improve this answer























          • Nice! this worked. Thank you!
            – Webodan
            2 days ago











          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',
          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
          });


          }
          });






          Webodan is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482752%2fdelete-folders-with-the-same-name-as-the-parent-folders-recursively-in-bash%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          If your find supports the -regex predicate, you can list the directories with:



          find . -type d -regex '.*/([^/]*)/1' -prune -print


          to remove them, you can change -print to:



          -exec rm -rf {} +


          But be sure to check the list first so you don't delete any files you need.






          share|improve this answer























          • Nice! this worked. Thank you!
            – Webodan
            2 days ago















          up vote
          1
          down vote



          accepted










          If your find supports the -regex predicate, you can list the directories with:



          find . -type d -regex '.*/([^/]*)/1' -prune -print


          to remove them, you can change -print to:



          -exec rm -rf {} +


          But be sure to check the list first so you don't delete any files you need.






          share|improve this answer























          • Nice! this worked. Thank you!
            – Webodan
            2 days ago













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          If your find supports the -regex predicate, you can list the directories with:



          find . -type d -regex '.*/([^/]*)/1' -prune -print


          to remove them, you can change -print to:



          -exec rm -rf {} +


          But be sure to check the list first so you don't delete any files you need.






          share|improve this answer














          If your find supports the -regex predicate, you can list the directories with:



          find . -type d -regex '.*/([^/]*)/1' -prune -print


          to remove them, you can change -print to:



          -exec rm -rf {} +


          But be sure to check the list first so you don't delete any files you need.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago









          Stéphane Chazelas

          294k54552894




          294k54552894










          answered 2 days ago









          choroba

          25.8k44470




          25.8k44470












          • Nice! this worked. Thank you!
            – Webodan
            2 days ago


















          • Nice! this worked. Thank you!
            – Webodan
            2 days ago
















          Nice! this worked. Thank you!
          – Webodan
          2 days ago




          Nice! this worked. Thank you!
          – Webodan
          2 days ago










          Webodan is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          Webodan is a new contributor. Be nice, and check out our Code of Conduct.













          Webodan is a new contributor. Be nice, and check out our Code of Conduct.












          Webodan is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482752%2fdelete-folders-with-the-same-name-as-the-parent-folders-recursively-in-bash%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

          サソリ

          広島県道265号伴広島線

          Accessing regular linux commands in Huawei's Dopra Linux