How to remove all files starting with a certain string in Linux












31















I need to find all files starting with the name NAME in a directory tree and remove all these files using one shell command.










share|improve this question















migrated from stackoverflow.com Oct 2 '12 at 13:40


This question came from our site for professional and enthusiast programmers.























    31















    I need to find all files starting with the name NAME in a directory tree and remove all these files using one shell command.










    share|improve this question















    migrated from stackoverflow.com Oct 2 '12 at 13:40


    This question came from our site for professional and enthusiast programmers.





















      31












      31








      31


      15






      I need to find all files starting with the name NAME in a directory tree and remove all these files using one shell command.










      share|improve this question
















      I need to find all files starting with the name NAME in a directory tree and remove all these files using one shell command.







      linux bash






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 2 '12 at 14:36









      slhck

      161k47447470




      161k47447470










      asked Oct 2 '12 at 12:18









      Achini PumikaAchini Pumika

      156124




      156124




      migrated from stackoverflow.com Oct 2 '12 at 13:40


      This question came from our site for professional and enthusiast programmers.









      migrated from stackoverflow.com Oct 2 '12 at 13:40


      This question came from our site for professional and enthusiast programmers.
























          7 Answers
          7






          active

          oldest

          votes


















          -1














          to delete all files which name has name--- you can use



          find -name . 'name*' -exec rm {} ;





          share|improve this answer





















          • 1





            You can also add -f as an 'rm' argument so you don't get prompted for "Are you sure you want to remove X file?"

            – UtahJarhead
            Oct 2 '12 at 14:01











          • Which version of grep has an -exec switch?

            – Ben Graham
            Oct 3 '12 at 2:55











          • Why does this comment have downvotes?

            – Ultrasonic54321
            Jan 30 at 17:07



















          60














          Delete all files in current directory and its sub-directories where the file name starts with "foo":



          $ find . -type f -name foo* -exec rm {} ;


          NB: use with caution - back up first - also do a dry run first, e.g.



          $ find . -type f -name foo*


          will just tell you the names of the files that would be deleted.






          share|improve this answer



















          • 4





            I had to delete over 2Million files and run in to trouble, find . -type f -name foo* -delete did the trick

            – Linas
            Jan 25 '14 at 13:30



















          14














          I have tried this way it is working for me try below command.



          rm -rf Example*


          here "Example" is text which is common for all files.






          share|improve this answer































            10














            You can use find:



            find . -name "name*" -exec rm {} ;





            share|improve this answer
























            • Also instead of specifying '.' you can specify an absolute path.

              – UtahJarhead
              Oct 2 '12 at 14:02



















            3














            With the globstar option (enable with shopt -s globstar):



            rm -f **/NAME*


            **/ expands to ./, */, */*/, */*/*/ etc. If you have a directory name starting with NAME, the command will complain that rm can't remove directories, but that's all.



            Notice that this might run into command line length limitations if the glob matches many files.



            Alternatively, with as few invocations of rm as possible, but not subject to any command line length limitations:



            find . -type f -name 'NAME*' -exec rm -f {} +


            (Notice the + instead of ; to close the -exec statement.)






            share|improve this answer

































              2














              find . -name 'foo'* -type f -delete seems like the simplest answer.



              You can run this without the -delete flag before to see which files will be deleted.






              share|improve this answer































                -2














                Search for the "Inode" number of the file/folder and then delete using inode number. Below is an example:



                ls -il
                3407873 drwxr-xr-x. 2 root root 4096 Mar 30 07:49 –p

                find . -inum 3407873 -exec rm -rf {} ;





                share|improve this answer


























                • This is a good answer — to a different question.  It’s not an answer to this question.

                  – Scott
                  Oct 26 '18 at 4:33











                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%2f482435%2fhow-to-remove-all-files-starting-with-a-certain-string-in-linux%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                7 Answers
                7






                active

                oldest

                votes








                7 Answers
                7






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                -1














                to delete all files which name has name--- you can use



                find -name . 'name*' -exec rm {} ;





                share|improve this answer





















                • 1





                  You can also add -f as an 'rm' argument so you don't get prompted for "Are you sure you want to remove X file?"

                  – UtahJarhead
                  Oct 2 '12 at 14:01











                • Which version of grep has an -exec switch?

                  – Ben Graham
                  Oct 3 '12 at 2:55











                • Why does this comment have downvotes?

                  – Ultrasonic54321
                  Jan 30 at 17:07
















                -1














                to delete all files which name has name--- you can use



                find -name . 'name*' -exec rm {} ;





                share|improve this answer





















                • 1





                  You can also add -f as an 'rm' argument so you don't get prompted for "Are you sure you want to remove X file?"

                  – UtahJarhead
                  Oct 2 '12 at 14:01











                • Which version of grep has an -exec switch?

                  – Ben Graham
                  Oct 3 '12 at 2:55











                • Why does this comment have downvotes?

                  – Ultrasonic54321
                  Jan 30 at 17:07














                -1












                -1








                -1







                to delete all files which name has name--- you can use



                find -name . 'name*' -exec rm {} ;





                share|improve this answer















                to delete all files which name has name--- you can use



                find -name . 'name*' -exec rm {} ;






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Oct 10 '12 at 12:14









                Community

                1




                1










                answered Oct 2 '12 at 12:48







                Dharshana















                • 1





                  You can also add -f as an 'rm' argument so you don't get prompted for "Are you sure you want to remove X file?"

                  – UtahJarhead
                  Oct 2 '12 at 14:01











                • Which version of grep has an -exec switch?

                  – Ben Graham
                  Oct 3 '12 at 2:55











                • Why does this comment have downvotes?

                  – Ultrasonic54321
                  Jan 30 at 17:07














                • 1





                  You can also add -f as an 'rm' argument so you don't get prompted for "Are you sure you want to remove X file?"

                  – UtahJarhead
                  Oct 2 '12 at 14:01











                • Which version of grep has an -exec switch?

                  – Ben Graham
                  Oct 3 '12 at 2:55











                • Why does this comment have downvotes?

                  – Ultrasonic54321
                  Jan 30 at 17:07








                1




                1





                You can also add -f as an 'rm' argument so you don't get prompted for "Are you sure you want to remove X file?"

                – UtahJarhead
                Oct 2 '12 at 14:01





                You can also add -f as an 'rm' argument so you don't get prompted for "Are you sure you want to remove X file?"

                – UtahJarhead
                Oct 2 '12 at 14:01













                Which version of grep has an -exec switch?

                – Ben Graham
                Oct 3 '12 at 2:55





                Which version of grep has an -exec switch?

                – Ben Graham
                Oct 3 '12 at 2:55













                Why does this comment have downvotes?

                – Ultrasonic54321
                Jan 30 at 17:07





                Why does this comment have downvotes?

                – Ultrasonic54321
                Jan 30 at 17:07













                60














                Delete all files in current directory and its sub-directories where the file name starts with "foo":



                $ find . -type f -name foo* -exec rm {} ;


                NB: use with caution - back up first - also do a dry run first, e.g.



                $ find . -type f -name foo*


                will just tell you the names of the files that would be deleted.






                share|improve this answer



















                • 4





                  I had to delete over 2Million files and run in to trouble, find . -type f -name foo* -delete did the trick

                  – Linas
                  Jan 25 '14 at 13:30
















                60














                Delete all files in current directory and its sub-directories where the file name starts with "foo":



                $ find . -type f -name foo* -exec rm {} ;


                NB: use with caution - back up first - also do a dry run first, e.g.



                $ find . -type f -name foo*


                will just tell you the names of the files that would be deleted.






                share|improve this answer



















                • 4





                  I had to delete over 2Million files and run in to trouble, find . -type f -name foo* -delete did the trick

                  – Linas
                  Jan 25 '14 at 13:30














                60












                60








                60







                Delete all files in current directory and its sub-directories where the file name starts with "foo":



                $ find . -type f -name foo* -exec rm {} ;


                NB: use with caution - back up first - also do a dry run first, e.g.



                $ find . -type f -name foo*


                will just tell you the names of the files that would be deleted.






                share|improve this answer













                Delete all files in current directory and its sub-directories where the file name starts with "foo":



                $ find . -type f -name foo* -exec rm {} ;


                NB: use with caution - back up first - also do a dry run first, e.g.



                $ find . -type f -name foo*


                will just tell you the names of the files that would be deleted.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Oct 2 '12 at 12:19









                Paul RPaul R

                4,28611727




                4,28611727








                • 4





                  I had to delete over 2Million files and run in to trouble, find . -type f -name foo* -delete did the trick

                  – Linas
                  Jan 25 '14 at 13:30














                • 4





                  I had to delete over 2Million files and run in to trouble, find . -type f -name foo* -delete did the trick

                  – Linas
                  Jan 25 '14 at 13:30








                4




                4





                I had to delete over 2Million files and run in to trouble, find . -type f -name foo* -delete did the trick

                – Linas
                Jan 25 '14 at 13:30





                I had to delete over 2Million files and run in to trouble, find . -type f -name foo* -delete did the trick

                – Linas
                Jan 25 '14 at 13:30











                14














                I have tried this way it is working for me try below command.



                rm -rf Example*


                here "Example" is text which is common for all files.






                share|improve this answer




























                  14














                  I have tried this way it is working for me try below command.



                  rm -rf Example*


                  here "Example" is text which is common for all files.






                  share|improve this answer


























                    14












                    14








                    14







                    I have tried this way it is working for me try below command.



                    rm -rf Example*


                    here "Example" is text which is common for all files.






                    share|improve this answer













                    I have tried this way it is working for me try below command.



                    rm -rf Example*


                    here "Example" is text which is common for all files.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jul 28 '16 at 10:51









                    Dinesh BhojvaniDinesh Bhojvani

                    24923




                    24923























                        10














                        You can use find:



                        find . -name "name*" -exec rm {} ;





                        share|improve this answer
























                        • Also instead of specifying '.' you can specify an absolute path.

                          – UtahJarhead
                          Oct 2 '12 at 14:02
















                        10














                        You can use find:



                        find . -name "name*" -exec rm {} ;





                        share|improve this answer
























                        • Also instead of specifying '.' you can specify an absolute path.

                          – UtahJarhead
                          Oct 2 '12 at 14:02














                        10












                        10








                        10







                        You can use find:



                        find . -name "name*" -exec rm {} ;





                        share|improve this answer













                        You can use find:



                        find . -name "name*" -exec rm {} ;






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Oct 2 '12 at 12:22









                        P.P.P.P.

                        45648




                        45648













                        • Also instead of specifying '.' you can specify an absolute path.

                          – UtahJarhead
                          Oct 2 '12 at 14:02



















                        • Also instead of specifying '.' you can specify an absolute path.

                          – UtahJarhead
                          Oct 2 '12 at 14:02

















                        Also instead of specifying '.' you can specify an absolute path.

                        – UtahJarhead
                        Oct 2 '12 at 14:02





                        Also instead of specifying '.' you can specify an absolute path.

                        – UtahJarhead
                        Oct 2 '12 at 14:02











                        3














                        With the globstar option (enable with shopt -s globstar):



                        rm -f **/NAME*


                        **/ expands to ./, */, */*/, */*/*/ etc. If you have a directory name starting with NAME, the command will complain that rm can't remove directories, but that's all.



                        Notice that this might run into command line length limitations if the glob matches many files.



                        Alternatively, with as few invocations of rm as possible, but not subject to any command line length limitations:



                        find . -type f -name 'NAME*' -exec rm -f {} +


                        (Notice the + instead of ; to close the -exec statement.)






                        share|improve this answer






























                          3














                          With the globstar option (enable with shopt -s globstar):



                          rm -f **/NAME*


                          **/ expands to ./, */, */*/, */*/*/ etc. If you have a directory name starting with NAME, the command will complain that rm can't remove directories, but that's all.



                          Notice that this might run into command line length limitations if the glob matches many files.



                          Alternatively, with as few invocations of rm as possible, but not subject to any command line length limitations:



                          find . -type f -name 'NAME*' -exec rm -f {} +


                          (Notice the + instead of ; to close the -exec statement.)






                          share|improve this answer




























                            3












                            3








                            3







                            With the globstar option (enable with shopt -s globstar):



                            rm -f **/NAME*


                            **/ expands to ./, */, */*/, */*/*/ etc. If you have a directory name starting with NAME, the command will complain that rm can't remove directories, but that's all.



                            Notice that this might run into command line length limitations if the glob matches many files.



                            Alternatively, with as few invocations of rm as possible, but not subject to any command line length limitations:



                            find . -type f -name 'NAME*' -exec rm -f {} +


                            (Notice the + instead of ; to close the -exec statement.)






                            share|improve this answer















                            With the globstar option (enable with shopt -s globstar):



                            rm -f **/NAME*


                            **/ expands to ./, */, */*/, */*/*/ etc. If you have a directory name starting with NAME, the command will complain that rm can't remove directories, but that's all.



                            Notice that this might run into command line length limitations if the glob matches many files.



                            Alternatively, with as few invocations of rm as possible, but not subject to any command line length limitations:



                            find . -type f -name 'NAME*' -exec rm -f {} +


                            (Notice the + instead of ; to close the -exec statement.)







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 30 at 17:05

























                            answered Feb 24 '17 at 3:17









                            Benjamin W.Benjamin W.

                            17918




                            17918























                                2














                                find . -name 'foo'* -type f -delete seems like the simplest answer.



                                You can run this without the -delete flag before to see which files will be deleted.






                                share|improve this answer




























                                  2














                                  find . -name 'foo'* -type f -delete seems like the simplest answer.



                                  You can run this without the -delete flag before to see which files will be deleted.






                                  share|improve this answer


























                                    2












                                    2








                                    2







                                    find . -name 'foo'* -type f -delete seems like the simplest answer.



                                    You can run this without the -delete flag before to see which files will be deleted.






                                    share|improve this answer













                                    find . -name 'foo'* -type f -delete seems like the simplest answer.



                                    You can run this without the -delete flag before to see which files will be deleted.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Aug 27 '18 at 16:28









                                    Yehuda SchwartzYehuda Schwartz

                                    1212




                                    1212























                                        -2














                                        Search for the "Inode" number of the file/folder and then delete using inode number. Below is an example:



                                        ls -il
                                        3407873 drwxr-xr-x. 2 root root 4096 Mar 30 07:49 –p

                                        find . -inum 3407873 -exec rm -rf {} ;





                                        share|improve this answer


























                                        • This is a good answer — to a different question.  It’s not an answer to this question.

                                          – Scott
                                          Oct 26 '18 at 4:33
















                                        -2














                                        Search for the "Inode" number of the file/folder and then delete using inode number. Below is an example:



                                        ls -il
                                        3407873 drwxr-xr-x. 2 root root 4096 Mar 30 07:49 –p

                                        find . -inum 3407873 -exec rm -rf {} ;





                                        share|improve this answer


























                                        • This is a good answer — to a different question.  It’s not an answer to this question.

                                          – Scott
                                          Oct 26 '18 at 4:33














                                        -2












                                        -2








                                        -2







                                        Search for the "Inode" number of the file/folder and then delete using inode number. Below is an example:



                                        ls -il
                                        3407873 drwxr-xr-x. 2 root root 4096 Mar 30 07:49 –p

                                        find . -inum 3407873 -exec rm -rf {} ;





                                        share|improve this answer















                                        Search for the "Inode" number of the file/folder and then delete using inode number. Below is an example:



                                        ls -il
                                        3407873 drwxr-xr-x. 2 root root 4096 Mar 30 07:49 –p

                                        find . -inum 3407873 -exec rm -rf {} ;






                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Mar 30 '15 at 6:12









                                        bummi

                                        1,48231422




                                        1,48231422










                                        answered Mar 30 '15 at 6:04









                                        DiwaDiwa

                                        52




                                        52













                                        • This is a good answer — to a different question.  It’s not an answer to this question.

                                          – Scott
                                          Oct 26 '18 at 4:33



















                                        • This is a good answer — to a different question.  It’s not an answer to this question.

                                          – Scott
                                          Oct 26 '18 at 4:33

















                                        This is a good answer — to a different question.  It’s not an answer to this question.

                                        – Scott
                                        Oct 26 '18 at 4:33





                                        This is a good answer — to a different question.  It’s not an answer to this question.

                                        – Scott
                                        Oct 26 '18 at 4:33


















                                        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.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f482435%2fhow-to-remove-all-files-starting-with-a-certain-string-in-linux%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 make a Squid Proxy server?

                                        Is this a new Fibonacci Identity?

                                        19世紀