bash - remove all directories (and contents) but not files in pwd












11














I'd like to remove all directories from the pwd but leave the files in the pwd alone. If the content of my pwd is:



mydir1
mydir2
myfile1
myfile2


then I'd like to be left with just



myfile1
myfile2


I assume that I need to use rm -r -i



Am I correct?










share|improve this question





























    11














    I'd like to remove all directories from the pwd but leave the files in the pwd alone. If the content of my pwd is:



    mydir1
    mydir2
    myfile1
    myfile2


    then I'd like to be left with just



    myfile1
    myfile2


    I assume that I need to use rm -r -i



    Am I correct?










    share|improve this question



























      11












      11








      11


      1





      I'd like to remove all directories from the pwd but leave the files in the pwd alone. If the content of my pwd is:



      mydir1
      mydir2
      myfile1
      myfile2


      then I'd like to be left with just



      myfile1
      myfile2


      I assume that I need to use rm -r -i



      Am I correct?










      share|improve this question















      I'd like to remove all directories from the pwd but leave the files in the pwd alone. If the content of my pwd is:



      mydir1
      mydir2
      myfile1
      myfile2


      then I'd like to be left with just



      myfile1
      myfile2


      I assume that I need to use rm -r -i



      Am I correct?







      linux bash mingw






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 7 '14 at 22:05









      lesmana

      12.9k53442




      12.9k53442










      asked Feb 7 '14 at 15:33









      atomh33ls

      3245924




      3245924






















          5 Answers
          5






          active

          oldest

          votes


















          7














          No that would give you "missing operand" since you didn't specify anything. Putting a "*" would prompt also for files.



          I'd give a try to:




          find -mindepth 1 -maxdepth 1 -type d -exec rm -r {} ;




          The "mindepth 1" will exclude "." from the results, the "maxdepth 1" will exclude trying to do under the directories that will anyway get deleted (therefore creating a warning). But in practice you could leave them both out if you agree to have a few "innocent" warnings.






          share|improve this answer





























            11














            I found this one somewhere:



            rm -r */


            Seems the easiest way to go. With your example, you would have to confirm each case, if you have 5 files it's OK, but with bigger file structures an interactive mode is't the way to go... Just as a suggestion, if it's important information, make a backup...






            share|improve this answer

















            • 1




              That will also follow symbolic links, which very probably isn't desired here.
              – JdeBP
              Feb 7 '14 at 16:43



















            5














            Use



            rm -rf ./*/


            That avoids interactive mode an deletes only directories in your local directory.






            share|improve this answer





















            • As J­de­B­P pointed out on Martin's very similar answer,  if the current (top-level) directory contains symbolic links to other directories, they will also be deleted (even if they aren't in or subordinate to the current directory).
              – Scott
              Dec 21 '18 at 7:07



















            3














            Something like this should work:



            find /path -type d -exec rm -rf '{}' ;



            -type d looks for only directories






            share|improve this answer





























              -1














              you can also try in this way to delete only all folders not files from any location in linux.

              #delete only all dir and don't touch files
              #!/bin/bash
              for dir in `ls -l | grep ^d | awk '{print $9}'`
              do
              echo "going to delete $dir " `rm -rf $dir`
              done
              ls





              share|improve this answer





















                Your Answer








                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "3"
                };
                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: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                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%2fsuperuser.com%2fquestions%2f713741%2fbash-remove-all-directories-and-contents-but-not-files-in-pwd%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









                7














                No that would give you "missing operand" since you didn't specify anything. Putting a "*" would prompt also for files.



                I'd give a try to:




                find -mindepth 1 -maxdepth 1 -type d -exec rm -r {} ;




                The "mindepth 1" will exclude "." from the results, the "maxdepth 1" will exclude trying to do under the directories that will anyway get deleted (therefore creating a warning). But in practice you could leave them both out if you agree to have a few "innocent" warnings.






                share|improve this answer


























                  7














                  No that would give you "missing operand" since you didn't specify anything. Putting a "*" would prompt also for files.



                  I'd give a try to:




                  find -mindepth 1 -maxdepth 1 -type d -exec rm -r {} ;




                  The "mindepth 1" will exclude "." from the results, the "maxdepth 1" will exclude trying to do under the directories that will anyway get deleted (therefore creating a warning). But in practice you could leave them both out if you agree to have a few "innocent" warnings.






                  share|improve this answer
























                    7












                    7








                    7






                    No that would give you "missing operand" since you didn't specify anything. Putting a "*" would prompt also for files.



                    I'd give a try to:




                    find -mindepth 1 -maxdepth 1 -type d -exec rm -r {} ;




                    The "mindepth 1" will exclude "." from the results, the "maxdepth 1" will exclude trying to do under the directories that will anyway get deleted (therefore creating a warning). But in practice you could leave them both out if you agree to have a few "innocent" warnings.






                    share|improve this answer












                    No that would give you "missing operand" since you didn't specify anything. Putting a "*" would prompt also for files.



                    I'd give a try to:




                    find -mindepth 1 -maxdepth 1 -type d -exec rm -r {} ;




                    The "mindepth 1" will exclude "." from the results, the "maxdepth 1" will exclude trying to do under the directories that will anyway get deleted (therefore creating a warning). But in practice you could leave them both out if you agree to have a few "innocent" warnings.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 7 '14 at 15:42









                    fede.evol

                    1,652175




                    1,652175

























                        11














                        I found this one somewhere:



                        rm -r */


                        Seems the easiest way to go. With your example, you would have to confirm each case, if you have 5 files it's OK, but with bigger file structures an interactive mode is't the way to go... Just as a suggestion, if it's important information, make a backup...






                        share|improve this answer

















                        • 1




                          That will also follow symbolic links, which very probably isn't desired here.
                          – JdeBP
                          Feb 7 '14 at 16:43
















                        11














                        I found this one somewhere:



                        rm -r */


                        Seems the easiest way to go. With your example, you would have to confirm each case, if you have 5 files it's OK, but with bigger file structures an interactive mode is't the way to go... Just as a suggestion, if it's important information, make a backup...






                        share|improve this answer

















                        • 1




                          That will also follow symbolic links, which very probably isn't desired here.
                          – JdeBP
                          Feb 7 '14 at 16:43














                        11












                        11








                        11






                        I found this one somewhere:



                        rm -r */


                        Seems the easiest way to go. With your example, you would have to confirm each case, if you have 5 files it's OK, but with bigger file structures an interactive mode is't the way to go... Just as a suggestion, if it's important information, make a backup...






                        share|improve this answer












                        I found this one somewhere:



                        rm -r */


                        Seems the easiest way to go. With your example, you would have to confirm each case, if you have 5 files it's OK, but with bigger file structures an interactive mode is't the way to go... Just as a suggestion, if it's important information, make a backup...







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Feb 7 '14 at 15:42









                        Martin

                        23115




                        23115








                        • 1




                          That will also follow symbolic links, which very probably isn't desired here.
                          – JdeBP
                          Feb 7 '14 at 16:43














                        • 1




                          That will also follow symbolic links, which very probably isn't desired here.
                          – JdeBP
                          Feb 7 '14 at 16:43








                        1




                        1




                        That will also follow symbolic links, which very probably isn't desired here.
                        – JdeBP
                        Feb 7 '14 at 16:43




                        That will also follow symbolic links, which very probably isn't desired here.
                        – JdeBP
                        Feb 7 '14 at 16:43











                        5














                        Use



                        rm -rf ./*/


                        That avoids interactive mode an deletes only directories in your local directory.






                        share|improve this answer





















                        • As J­de­B­P pointed out on Martin's very similar answer,  if the current (top-level) directory contains symbolic links to other directories, they will also be deleted (even if they aren't in or subordinate to the current directory).
                          – Scott
                          Dec 21 '18 at 7:07
















                        5














                        Use



                        rm -rf ./*/


                        That avoids interactive mode an deletes only directories in your local directory.






                        share|improve this answer





















                        • As J­de­B­P pointed out on Martin's very similar answer,  if the current (top-level) directory contains symbolic links to other directories, they will also be deleted (even if they aren't in or subordinate to the current directory).
                          – Scott
                          Dec 21 '18 at 7:07














                        5












                        5








                        5






                        Use



                        rm -rf ./*/


                        That avoids interactive mode an deletes only directories in your local directory.






                        share|improve this answer












                        Use



                        rm -rf ./*/


                        That avoids interactive mode an deletes only directories in your local directory.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Feb 8 '14 at 16:15









                        WeSee

                        21612




                        21612












                        • As J­de­B­P pointed out on Martin's very similar answer,  if the current (top-level) directory contains symbolic links to other directories, they will also be deleted (even if they aren't in or subordinate to the current directory).
                          – Scott
                          Dec 21 '18 at 7:07


















                        • As J­de­B­P pointed out on Martin's very similar answer,  if the current (top-level) directory contains symbolic links to other directories, they will also be deleted (even if they aren't in or subordinate to the current directory).
                          – Scott
                          Dec 21 '18 at 7:07
















                        As J­de­B­P pointed out on Martin's very similar answer,  if the current (top-level) directory contains symbolic links to other directories, they will also be deleted (even if they aren't in or subordinate to the current directory).
                        – Scott
                        Dec 21 '18 at 7:07




                        As J­de­B­P pointed out on Martin's very similar answer,  if the current (top-level) directory contains symbolic links to other directories, they will also be deleted (even if they aren't in or subordinate to the current directory).
                        – Scott
                        Dec 21 '18 at 7:07











                        3














                        Something like this should work:



                        find /path -type d -exec rm -rf '{}' ;



                        -type d looks for only directories






                        share|improve this answer


























                          3














                          Something like this should work:



                          find /path -type d -exec rm -rf '{}' ;



                          -type d looks for only directories






                          share|improve this answer
























                            3












                            3








                            3






                            Something like this should work:



                            find /path -type d -exec rm -rf '{}' ;



                            -type d looks for only directories






                            share|improve this answer












                            Something like this should work:



                            find /path -type d -exec rm -rf '{}' ;



                            -type d looks for only directories







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 7 '14 at 15:45









                            Matthew Williams

                            4,00182036




                            4,00182036























                                -1














                                you can also try in this way to delete only all folders not files from any location in linux.

                                #delete only all dir and don't touch files
                                #!/bin/bash
                                for dir in `ls -l | grep ^d | awk '{print $9}'`
                                do
                                echo "going to delete $dir " `rm -rf $dir`
                                done
                                ls





                                share|improve this answer


























                                  -1














                                  you can also try in this way to delete only all folders not files from any location in linux.

                                  #delete only all dir and don't touch files
                                  #!/bin/bash
                                  for dir in `ls -l | grep ^d | awk '{print $9}'`
                                  do
                                  echo "going to delete $dir " `rm -rf $dir`
                                  done
                                  ls





                                  share|improve this answer
























                                    -1












                                    -1








                                    -1






                                    you can also try in this way to delete only all folders not files from any location in linux.

                                    #delete only all dir and don't touch files
                                    #!/bin/bash
                                    for dir in `ls -l | grep ^d | awk '{print $9}'`
                                    do
                                    echo "going to delete $dir " `rm -rf $dir`
                                    done
                                    ls





                                    share|improve this answer












                                    you can also try in this way to delete only all folders not files from any location in linux.

                                    #delete only all dir and don't touch files
                                    #!/bin/bash
                                    for dir in `ls -l | grep ^d | awk '{print $9}'`
                                    do
                                    echo "going to delete $dir " `rm -rf $dir`
                                    done
                                    ls






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Dec 21 '18 at 2:09









                                    linux.cnf

                                    1




                                    1






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Super User!


                                        • 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.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • 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%2fsuperuser.com%2fquestions%2f713741%2fbash-remove-all-directories-and-contents-but-not-files-in-pwd%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

                                        flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                                        Mangá

                                        Eduardo VII do Reino Unido