How can I recursively delete all files of a specific extension in the current directory?











up vote
437
down vote

favorite
181












How do I safely delete all files with a specific extension (e.g. .bak) from current directory and all subfolders using one command-line? Simply, I'm afraid to use rm since I used it wrong once and now I need advice.










share|improve this question




























    up vote
    437
    down vote

    favorite
    181












    How do I safely delete all files with a specific extension (e.g. .bak) from current directory and all subfolders using one command-line? Simply, I'm afraid to use rm since I used it wrong once and now I need advice.










    share|improve this question


























      up vote
      437
      down vote

      favorite
      181









      up vote
      437
      down vote

      favorite
      181






      181





      How do I safely delete all files with a specific extension (e.g. .bak) from current directory and all subfolders using one command-line? Simply, I'm afraid to use rm since I used it wrong once and now I need advice.










      share|improve this question















      How do I safely delete all files with a specific extension (e.g. .bak) from current directory and all subfolders using one command-line? Simply, I'm afraid to use rm since I used it wrong once and now I need advice.







      command-line files rm batch






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '13 at 16:16









      Glutanimate

      16k872130




      16k872130










      asked Nov 15 '13 at 13:03









      user216038

      2,188384




      2,188384






















          6 Answers
          6






          active

          oldest

          votes

















          up vote
          684
          down vote



          accepted










          You don't even need to use rm in this case if you are afraid. Use find:



          find . -name "*.bak" -type f -delete


          But use it with precaution. Run first:



          find . -name "*.bak" -type f


          to see exactly which files you will remove.



          Also, make sure that -delete is the last argument in your command. If you put it before the -name *.bak argument, it will delete everything.



          See man find and man rm for more info and see also this related question on SE:




          • How do I remove all .pyc files from a project?






          share|improve this answer























          • How's this different from rm *.bak?
            – sayantankhan
            Nov 15 '13 at 13:11






          • 8




            @Bolt64 Your rm *.bak will not work for subdirectories.
            – Radu Rădeanu
            Nov 15 '13 at 13:14












          • With default settings rm *.bak will only delete all files ending with .bak in the current directory. TO also do things in subdirectories you either needed to fiddle with globs, use the -r option or use the find example.
            – Hennes
            Nov 15 '13 at 13:14






          • 7




            @Hennes Be careful with rm -r *.bak! It also removes directories ending in .bak with all their content.
            – Radu Rădeanu
            Nov 15 '13 at 13:34








          • 30




            Make sure that -delete is the last argument in your command. If you put it before the -name *.bak argument, it will delete everything.
            – Michael
            Oct 29 '14 at 14:36


















          up vote
          32
          down vote













          find . -name "*.bak" -type f -print0 | xargs -0 /bin/rm -f





          share|improve this answer



















          • 1




            Welcome to askubuntu! While this is a perfectly valid answer I don't think there's any advantage in using this instead of the -delete flag of find. More information can be found in the GNU manuals entry for deleting files with find.
            – Glutanimate
            Apr 4 '14 at 20:11






          • 1




            you are probably right, it's just an alternative solution, perhaps more raw ;)
            – lokers
            Apr 4 '14 at 22:13






          • 9




            This is not just an alternative but an example how other commands can be combined together with the pipe '|'. +1
            – Boris Pavlović
            Jun 5 '14 at 7:18






          • 11




            This alternative solutions work on other environments that lack -delete (like cygwin)
            – ciriarte
            Aug 15 '14 at 4:07






          • 2




            I think this is the best answer here.
            – Léo Léopold Hertz 준영
            Jun 29 '15 at 11:27


















          up vote
          29
          down vote













          First run the command shopt -s globstar. You can run that on the command line, and it'll have effect only in that shell window. You can put it in your .bashrc, and then all newly started shells will pick it up. The effect of that command is to make **/ match files in the current directory and its subdirectories recursively (by default, **/ means the same thing as */: only in the immediate subdirectories). Then:



          rm **/*.bak


          (or gvfs-trash **/*.bak or what have you).






          share|improve this answer




























            up vote
            19
            down vote













            Deleting files is for me not something you should use rm for. Here is an alternative:



            sudo apt-get install gvfs     # install a tool that allows you to put stuff in the trash
            alias "trash"="gvfs-trash" # you can also put this in .bash_aliases or simply use the command without alias
            trash *.bak # trash the files (thus moving them to the trash bin)


            As Flimm states in the comments:




            The package trash-cli does the same thing as gvfs-trash without the dependency on gvfs.




            So:



            sudo apt-get install trash-cli


            You don't need to make an alias for this, because the trash-cli package provides a command trash, which does what we want.



            As Eliah Kagan makes clear in extensive comments, you can also make this recursive using find. In that case you can't use an alias, so the commands below assume you have installed trash-cli. I summarise Eliah's comments:



            This command finds and displays all .bak files and symlinks anywhere in the current directory or its subdirectories or below.



            find . -name '*.bak' -xtype f


            To delete them, append an -exec with the trash command:



            find . -name '*.bak' -xtype f -exec trash {} +


            -xtype f selects files and symlinks to files, but not folders. To delete .bak folders too, remove that part, and use -execdir, which avoids cannot trash non-existent errors for .bak files inside .bak directories:



            find . -name '*.bak' -execdir trash {} +





            share|improve this answer



















            • 3




              "Don't use rm to delete things" is a controversial statement but I have to agree that it's often wiser to use something that will let you undo things.
              – Oli
              Nov 15 '13 at 15:12






            • 2




              The package trash-cli does the same thing as gvfs-trash without the dependency on gvfs.
              – Flimm
              Nov 20 '13 at 9:08










            • I have edited it in the answer, next time feel free to do the edit yourself.
              – don.joey
              Nov 20 '13 at 10:06










            • @don.joey This answer seems to say find . -name "*.bak" -type f displays what trash *.bak deletes. Is that really what you mean? You can move directories to the trash with trash or gvfs-trash, but trash *.bak will only moves files and directories whose names end with .bak and that reside immediately in the current directory. The shell expands *.bak, so trash *.bak won't affect .bak files in subdirectories not themselves named .bak.
              – Eliah Kagan
              Oct 14 '17 at 2:30








            • 1




              @don.joey Yes ls *.bak (which I think you mean) lists what trash *.bak trashes. find . -name '*.bak' -xtype f -exec trash {} + trashes all .bak files anywhere under .. It can't use an alias, so install trash-cli or write gvfs-trash instead. Here's an example. -xtype f selects files and symlinks to files, but not folders. To delete .bak folders too, use find . -name '*.bak' -execdir trash {} +, which avoids cannot trash non existent errors for .bak files inside .bak directories. Please feel free to use any of this in your answer.
              – Eliah Kagan
              Oct 14 '17 at 19:15


















            up vote
            3
            down vote













            If you want to delete all files of a certain type, but only 1 folder "deep" from the current folder:



            find . -maxdepth 2 -name "*.log" -type f -delete


            -maxdepth 2 because the current directory "." counts as the first folder.






            share|improve this answer




























              up vote
              0
              down vote













              If you are inside a git repo, you can use:



              git clean -fdx


              This deletes untracked files and files in .gitignore.






              share|improve this answer





















                Your Answer








                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "89"
                };
                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: 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%2faskubuntu.com%2fquestions%2f377438%2fhow-can-i-recursively-delete-all-files-of-a-specific-extension-in-the-current-di%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                6 Answers
                6






                active

                oldest

                votes








                6 Answers
                6






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                684
                down vote



                accepted










                You don't even need to use rm in this case if you are afraid. Use find:



                find . -name "*.bak" -type f -delete


                But use it with precaution. Run first:



                find . -name "*.bak" -type f


                to see exactly which files you will remove.



                Also, make sure that -delete is the last argument in your command. If you put it before the -name *.bak argument, it will delete everything.



                See man find and man rm for more info and see also this related question on SE:




                • How do I remove all .pyc files from a project?






                share|improve this answer























                • How's this different from rm *.bak?
                  – sayantankhan
                  Nov 15 '13 at 13:11






                • 8




                  @Bolt64 Your rm *.bak will not work for subdirectories.
                  – Radu Rădeanu
                  Nov 15 '13 at 13:14












                • With default settings rm *.bak will only delete all files ending with .bak in the current directory. TO also do things in subdirectories you either needed to fiddle with globs, use the -r option or use the find example.
                  – Hennes
                  Nov 15 '13 at 13:14






                • 7




                  @Hennes Be careful with rm -r *.bak! It also removes directories ending in .bak with all their content.
                  – Radu Rădeanu
                  Nov 15 '13 at 13:34








                • 30




                  Make sure that -delete is the last argument in your command. If you put it before the -name *.bak argument, it will delete everything.
                  – Michael
                  Oct 29 '14 at 14:36















                up vote
                684
                down vote



                accepted










                You don't even need to use rm in this case if you are afraid. Use find:



                find . -name "*.bak" -type f -delete


                But use it with precaution. Run first:



                find . -name "*.bak" -type f


                to see exactly which files you will remove.



                Also, make sure that -delete is the last argument in your command. If you put it before the -name *.bak argument, it will delete everything.



                See man find and man rm for more info and see also this related question on SE:




                • How do I remove all .pyc files from a project?






                share|improve this answer























                • How's this different from rm *.bak?
                  – sayantankhan
                  Nov 15 '13 at 13:11






                • 8




                  @Bolt64 Your rm *.bak will not work for subdirectories.
                  – Radu Rădeanu
                  Nov 15 '13 at 13:14












                • With default settings rm *.bak will only delete all files ending with .bak in the current directory. TO also do things in subdirectories you either needed to fiddle with globs, use the -r option or use the find example.
                  – Hennes
                  Nov 15 '13 at 13:14






                • 7




                  @Hennes Be careful with rm -r *.bak! It also removes directories ending in .bak with all their content.
                  – Radu Rădeanu
                  Nov 15 '13 at 13:34








                • 30




                  Make sure that -delete is the last argument in your command. If you put it before the -name *.bak argument, it will delete everything.
                  – Michael
                  Oct 29 '14 at 14:36













                up vote
                684
                down vote



                accepted







                up vote
                684
                down vote



                accepted






                You don't even need to use rm in this case if you are afraid. Use find:



                find . -name "*.bak" -type f -delete


                But use it with precaution. Run first:



                find . -name "*.bak" -type f


                to see exactly which files you will remove.



                Also, make sure that -delete is the last argument in your command. If you put it before the -name *.bak argument, it will delete everything.



                See man find and man rm for more info and see also this related question on SE:




                • How do I remove all .pyc files from a project?






                share|improve this answer














                You don't even need to use rm in this case if you are afraid. Use find:



                find . -name "*.bak" -type f -delete


                But use it with precaution. Run first:



                find . -name "*.bak" -type f


                to see exactly which files you will remove.



                Also, make sure that -delete is the last argument in your command. If you put it before the -name *.bak argument, it will delete everything.



                See man find and man rm for more info and see also this related question on SE:




                • How do I remove all .pyc files from a project?







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited May 23 '17 at 12:39









                Community

                1




                1










                answered Nov 15 '13 at 13:08









                Radu Rădeanu

                114k34243321




                114k34243321












                • How's this different from rm *.bak?
                  – sayantankhan
                  Nov 15 '13 at 13:11






                • 8




                  @Bolt64 Your rm *.bak will not work for subdirectories.
                  – Radu Rădeanu
                  Nov 15 '13 at 13:14












                • With default settings rm *.bak will only delete all files ending with .bak in the current directory. TO also do things in subdirectories you either needed to fiddle with globs, use the -r option or use the find example.
                  – Hennes
                  Nov 15 '13 at 13:14






                • 7




                  @Hennes Be careful with rm -r *.bak! It also removes directories ending in .bak with all their content.
                  – Radu Rădeanu
                  Nov 15 '13 at 13:34








                • 30




                  Make sure that -delete is the last argument in your command. If you put it before the -name *.bak argument, it will delete everything.
                  – Michael
                  Oct 29 '14 at 14:36


















                • How's this different from rm *.bak?
                  – sayantankhan
                  Nov 15 '13 at 13:11






                • 8




                  @Bolt64 Your rm *.bak will not work for subdirectories.
                  – Radu Rădeanu
                  Nov 15 '13 at 13:14












                • With default settings rm *.bak will only delete all files ending with .bak in the current directory. TO also do things in subdirectories you either needed to fiddle with globs, use the -r option or use the find example.
                  – Hennes
                  Nov 15 '13 at 13:14






                • 7




                  @Hennes Be careful with rm -r *.bak! It also removes directories ending in .bak with all their content.
                  – Radu Rădeanu
                  Nov 15 '13 at 13:34








                • 30




                  Make sure that -delete is the last argument in your command. If you put it before the -name *.bak argument, it will delete everything.
                  – Michael
                  Oct 29 '14 at 14:36
















                How's this different from rm *.bak?
                – sayantankhan
                Nov 15 '13 at 13:11




                How's this different from rm *.bak?
                – sayantankhan
                Nov 15 '13 at 13:11




                8




                8




                @Bolt64 Your rm *.bak will not work for subdirectories.
                – Radu Rădeanu
                Nov 15 '13 at 13:14






                @Bolt64 Your rm *.bak will not work for subdirectories.
                – Radu Rădeanu
                Nov 15 '13 at 13:14














                With default settings rm *.bak will only delete all files ending with .bak in the current directory. TO also do things in subdirectories you either needed to fiddle with globs, use the -r option or use the find example.
                – Hennes
                Nov 15 '13 at 13:14




                With default settings rm *.bak will only delete all files ending with .bak in the current directory. TO also do things in subdirectories you either needed to fiddle with globs, use the -r option or use the find example.
                – Hennes
                Nov 15 '13 at 13:14




                7




                7




                @Hennes Be careful with rm -r *.bak! It also removes directories ending in .bak with all their content.
                – Radu Rădeanu
                Nov 15 '13 at 13:34






                @Hennes Be careful with rm -r *.bak! It also removes directories ending in .bak with all their content.
                – Radu Rădeanu
                Nov 15 '13 at 13:34






                30




                30




                Make sure that -delete is the last argument in your command. If you put it before the -name *.bak argument, it will delete everything.
                – Michael
                Oct 29 '14 at 14:36




                Make sure that -delete is the last argument in your command. If you put it before the -name *.bak argument, it will delete everything.
                – Michael
                Oct 29 '14 at 14:36












                up vote
                32
                down vote













                find . -name "*.bak" -type f -print0 | xargs -0 /bin/rm -f





                share|improve this answer



















                • 1




                  Welcome to askubuntu! While this is a perfectly valid answer I don't think there's any advantage in using this instead of the -delete flag of find. More information can be found in the GNU manuals entry for deleting files with find.
                  – Glutanimate
                  Apr 4 '14 at 20:11






                • 1




                  you are probably right, it's just an alternative solution, perhaps more raw ;)
                  – lokers
                  Apr 4 '14 at 22:13






                • 9




                  This is not just an alternative but an example how other commands can be combined together with the pipe '|'. +1
                  – Boris Pavlović
                  Jun 5 '14 at 7:18






                • 11




                  This alternative solutions work on other environments that lack -delete (like cygwin)
                  – ciriarte
                  Aug 15 '14 at 4:07






                • 2




                  I think this is the best answer here.
                  – Léo Léopold Hertz 준영
                  Jun 29 '15 at 11:27















                up vote
                32
                down vote













                find . -name "*.bak" -type f -print0 | xargs -0 /bin/rm -f





                share|improve this answer



















                • 1




                  Welcome to askubuntu! While this is a perfectly valid answer I don't think there's any advantage in using this instead of the -delete flag of find. More information can be found in the GNU manuals entry for deleting files with find.
                  – Glutanimate
                  Apr 4 '14 at 20:11






                • 1




                  you are probably right, it's just an alternative solution, perhaps more raw ;)
                  – lokers
                  Apr 4 '14 at 22:13






                • 9




                  This is not just an alternative but an example how other commands can be combined together with the pipe '|'. +1
                  – Boris Pavlović
                  Jun 5 '14 at 7:18






                • 11




                  This alternative solutions work on other environments that lack -delete (like cygwin)
                  – ciriarte
                  Aug 15 '14 at 4:07






                • 2




                  I think this is the best answer here.
                  – Léo Léopold Hertz 준영
                  Jun 29 '15 at 11:27













                up vote
                32
                down vote










                up vote
                32
                down vote









                find . -name "*.bak" -type f -print0 | xargs -0 /bin/rm -f





                share|improve this answer














                find . -name "*.bak" -type f -print0 | xargs -0 /bin/rm -f






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 8 '15 at 16:13









                muru

                133k19282479




                133k19282479










                answered Apr 4 '14 at 19:10









                lokers

                42942




                42942








                • 1




                  Welcome to askubuntu! While this is a perfectly valid answer I don't think there's any advantage in using this instead of the -delete flag of find. More information can be found in the GNU manuals entry for deleting files with find.
                  – Glutanimate
                  Apr 4 '14 at 20:11






                • 1




                  you are probably right, it's just an alternative solution, perhaps more raw ;)
                  – lokers
                  Apr 4 '14 at 22:13






                • 9




                  This is not just an alternative but an example how other commands can be combined together with the pipe '|'. +1
                  – Boris Pavlović
                  Jun 5 '14 at 7:18






                • 11




                  This alternative solutions work on other environments that lack -delete (like cygwin)
                  – ciriarte
                  Aug 15 '14 at 4:07






                • 2




                  I think this is the best answer here.
                  – Léo Léopold Hertz 준영
                  Jun 29 '15 at 11:27














                • 1




                  Welcome to askubuntu! While this is a perfectly valid answer I don't think there's any advantage in using this instead of the -delete flag of find. More information can be found in the GNU manuals entry for deleting files with find.
                  – Glutanimate
                  Apr 4 '14 at 20:11






                • 1




                  you are probably right, it's just an alternative solution, perhaps more raw ;)
                  – lokers
                  Apr 4 '14 at 22:13






                • 9




                  This is not just an alternative but an example how other commands can be combined together with the pipe '|'. +1
                  – Boris Pavlović
                  Jun 5 '14 at 7:18






                • 11




                  This alternative solutions work on other environments that lack -delete (like cygwin)
                  – ciriarte
                  Aug 15 '14 at 4:07






                • 2




                  I think this is the best answer here.
                  – Léo Léopold Hertz 준영
                  Jun 29 '15 at 11:27








                1




                1




                Welcome to askubuntu! While this is a perfectly valid answer I don't think there's any advantage in using this instead of the -delete flag of find. More information can be found in the GNU manuals entry for deleting files with find.
                – Glutanimate
                Apr 4 '14 at 20:11




                Welcome to askubuntu! While this is a perfectly valid answer I don't think there's any advantage in using this instead of the -delete flag of find. More information can be found in the GNU manuals entry for deleting files with find.
                – Glutanimate
                Apr 4 '14 at 20:11




                1




                1




                you are probably right, it's just an alternative solution, perhaps more raw ;)
                – lokers
                Apr 4 '14 at 22:13




                you are probably right, it's just an alternative solution, perhaps more raw ;)
                – lokers
                Apr 4 '14 at 22:13




                9




                9




                This is not just an alternative but an example how other commands can be combined together with the pipe '|'. +1
                – Boris Pavlović
                Jun 5 '14 at 7:18




                This is not just an alternative but an example how other commands can be combined together with the pipe '|'. +1
                – Boris Pavlović
                Jun 5 '14 at 7:18




                11




                11




                This alternative solutions work on other environments that lack -delete (like cygwin)
                – ciriarte
                Aug 15 '14 at 4:07




                This alternative solutions work on other environments that lack -delete (like cygwin)
                – ciriarte
                Aug 15 '14 at 4:07




                2




                2




                I think this is the best answer here.
                – Léo Léopold Hertz 준영
                Jun 29 '15 at 11:27




                I think this is the best answer here.
                – Léo Léopold Hertz 준영
                Jun 29 '15 at 11:27










                up vote
                29
                down vote













                First run the command shopt -s globstar. You can run that on the command line, and it'll have effect only in that shell window. You can put it in your .bashrc, and then all newly started shells will pick it up. The effect of that command is to make **/ match files in the current directory and its subdirectories recursively (by default, **/ means the same thing as */: only in the immediate subdirectories). Then:



                rm **/*.bak


                (or gvfs-trash **/*.bak or what have you).






                share|improve this answer

























                  up vote
                  29
                  down vote













                  First run the command shopt -s globstar. You can run that on the command line, and it'll have effect only in that shell window. You can put it in your .bashrc, and then all newly started shells will pick it up. The effect of that command is to make **/ match files in the current directory and its subdirectories recursively (by default, **/ means the same thing as */: only in the immediate subdirectories). Then:



                  rm **/*.bak


                  (or gvfs-trash **/*.bak or what have you).






                  share|improve this answer























                    up vote
                    29
                    down vote










                    up vote
                    29
                    down vote









                    First run the command shopt -s globstar. You can run that on the command line, and it'll have effect only in that shell window. You can put it in your .bashrc, and then all newly started shells will pick it up. The effect of that command is to make **/ match files in the current directory and its subdirectories recursively (by default, **/ means the same thing as */: only in the immediate subdirectories). Then:



                    rm **/*.bak


                    (or gvfs-trash **/*.bak or what have you).






                    share|improve this answer












                    First run the command shopt -s globstar. You can run that on the command line, and it'll have effect only in that shell window. You can put it in your .bashrc, and then all newly started shells will pick it up. The effect of that command is to make **/ match files in the current directory and its subdirectories recursively (by default, **/ means the same thing as */: only in the immediate subdirectories). Then:



                    rm **/*.bak


                    (or gvfs-trash **/*.bak or what have you).







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 15 '13 at 18:59









                    Gilles

                    44.1k1398137




                    44.1k1398137






















                        up vote
                        19
                        down vote













                        Deleting files is for me not something you should use rm for. Here is an alternative:



                        sudo apt-get install gvfs     # install a tool that allows you to put stuff in the trash
                        alias "trash"="gvfs-trash" # you can also put this in .bash_aliases or simply use the command without alias
                        trash *.bak # trash the files (thus moving them to the trash bin)


                        As Flimm states in the comments:




                        The package trash-cli does the same thing as gvfs-trash without the dependency on gvfs.




                        So:



                        sudo apt-get install trash-cli


                        You don't need to make an alias for this, because the trash-cli package provides a command trash, which does what we want.



                        As Eliah Kagan makes clear in extensive comments, you can also make this recursive using find. In that case you can't use an alias, so the commands below assume you have installed trash-cli. I summarise Eliah's comments:



                        This command finds and displays all .bak files and symlinks anywhere in the current directory or its subdirectories or below.



                        find . -name '*.bak' -xtype f


                        To delete them, append an -exec with the trash command:



                        find . -name '*.bak' -xtype f -exec trash {} +


                        -xtype f selects files and symlinks to files, but not folders. To delete .bak folders too, remove that part, and use -execdir, which avoids cannot trash non-existent errors for .bak files inside .bak directories:



                        find . -name '*.bak' -execdir trash {} +





                        share|improve this answer



















                        • 3




                          "Don't use rm to delete things" is a controversial statement but I have to agree that it's often wiser to use something that will let you undo things.
                          – Oli
                          Nov 15 '13 at 15:12






                        • 2




                          The package trash-cli does the same thing as gvfs-trash without the dependency on gvfs.
                          – Flimm
                          Nov 20 '13 at 9:08










                        • I have edited it in the answer, next time feel free to do the edit yourself.
                          – don.joey
                          Nov 20 '13 at 10:06










                        • @don.joey This answer seems to say find . -name "*.bak" -type f displays what trash *.bak deletes. Is that really what you mean? You can move directories to the trash with trash or gvfs-trash, but trash *.bak will only moves files and directories whose names end with .bak and that reside immediately in the current directory. The shell expands *.bak, so trash *.bak won't affect .bak files in subdirectories not themselves named .bak.
                          – Eliah Kagan
                          Oct 14 '17 at 2:30








                        • 1




                          @don.joey Yes ls *.bak (which I think you mean) lists what trash *.bak trashes. find . -name '*.bak' -xtype f -exec trash {} + trashes all .bak files anywhere under .. It can't use an alias, so install trash-cli or write gvfs-trash instead. Here's an example. -xtype f selects files and symlinks to files, but not folders. To delete .bak folders too, use find . -name '*.bak' -execdir trash {} +, which avoids cannot trash non existent errors for .bak files inside .bak directories. Please feel free to use any of this in your answer.
                          – Eliah Kagan
                          Oct 14 '17 at 19:15















                        up vote
                        19
                        down vote













                        Deleting files is for me not something you should use rm for. Here is an alternative:



                        sudo apt-get install gvfs     # install a tool that allows you to put stuff in the trash
                        alias "trash"="gvfs-trash" # you can also put this in .bash_aliases or simply use the command without alias
                        trash *.bak # trash the files (thus moving them to the trash bin)


                        As Flimm states in the comments:




                        The package trash-cli does the same thing as gvfs-trash without the dependency on gvfs.




                        So:



                        sudo apt-get install trash-cli


                        You don't need to make an alias for this, because the trash-cli package provides a command trash, which does what we want.



                        As Eliah Kagan makes clear in extensive comments, you can also make this recursive using find. In that case you can't use an alias, so the commands below assume you have installed trash-cli. I summarise Eliah's comments:



                        This command finds and displays all .bak files and symlinks anywhere in the current directory or its subdirectories or below.



                        find . -name '*.bak' -xtype f


                        To delete them, append an -exec with the trash command:



                        find . -name '*.bak' -xtype f -exec trash {} +


                        -xtype f selects files and symlinks to files, but not folders. To delete .bak folders too, remove that part, and use -execdir, which avoids cannot trash non-existent errors for .bak files inside .bak directories:



                        find . -name '*.bak' -execdir trash {} +





                        share|improve this answer



















                        • 3




                          "Don't use rm to delete things" is a controversial statement but I have to agree that it's often wiser to use something that will let you undo things.
                          – Oli
                          Nov 15 '13 at 15:12






                        • 2




                          The package trash-cli does the same thing as gvfs-trash without the dependency on gvfs.
                          – Flimm
                          Nov 20 '13 at 9:08










                        • I have edited it in the answer, next time feel free to do the edit yourself.
                          – don.joey
                          Nov 20 '13 at 10:06










                        • @don.joey This answer seems to say find . -name "*.bak" -type f displays what trash *.bak deletes. Is that really what you mean? You can move directories to the trash with trash or gvfs-trash, but trash *.bak will only moves files and directories whose names end with .bak and that reside immediately in the current directory. The shell expands *.bak, so trash *.bak won't affect .bak files in subdirectories not themselves named .bak.
                          – Eliah Kagan
                          Oct 14 '17 at 2:30








                        • 1




                          @don.joey Yes ls *.bak (which I think you mean) lists what trash *.bak trashes. find . -name '*.bak' -xtype f -exec trash {} + trashes all .bak files anywhere under .. It can't use an alias, so install trash-cli or write gvfs-trash instead. Here's an example. -xtype f selects files and symlinks to files, but not folders. To delete .bak folders too, use find . -name '*.bak' -execdir trash {} +, which avoids cannot trash non existent errors for .bak files inside .bak directories. Please feel free to use any of this in your answer.
                          – Eliah Kagan
                          Oct 14 '17 at 19:15













                        up vote
                        19
                        down vote










                        up vote
                        19
                        down vote









                        Deleting files is for me not something you should use rm for. Here is an alternative:



                        sudo apt-get install gvfs     # install a tool that allows you to put stuff in the trash
                        alias "trash"="gvfs-trash" # you can also put this in .bash_aliases or simply use the command without alias
                        trash *.bak # trash the files (thus moving them to the trash bin)


                        As Flimm states in the comments:




                        The package trash-cli does the same thing as gvfs-trash without the dependency on gvfs.




                        So:



                        sudo apt-get install trash-cli


                        You don't need to make an alias for this, because the trash-cli package provides a command trash, which does what we want.



                        As Eliah Kagan makes clear in extensive comments, you can also make this recursive using find. In that case you can't use an alias, so the commands below assume you have installed trash-cli. I summarise Eliah's comments:



                        This command finds and displays all .bak files and symlinks anywhere in the current directory or its subdirectories or below.



                        find . -name '*.bak' -xtype f


                        To delete them, append an -exec with the trash command:



                        find . -name '*.bak' -xtype f -exec trash {} +


                        -xtype f selects files and symlinks to files, but not folders. To delete .bak folders too, remove that part, and use -execdir, which avoids cannot trash non-existent errors for .bak files inside .bak directories:



                        find . -name '*.bak' -execdir trash {} +





                        share|improve this answer














                        Deleting files is for me not something you should use rm for. Here is an alternative:



                        sudo apt-get install gvfs     # install a tool that allows you to put stuff in the trash
                        alias "trash"="gvfs-trash" # you can also put this in .bash_aliases or simply use the command without alias
                        trash *.bak # trash the files (thus moving them to the trash bin)


                        As Flimm states in the comments:




                        The package trash-cli does the same thing as gvfs-trash without the dependency on gvfs.




                        So:



                        sudo apt-get install trash-cli


                        You don't need to make an alias for this, because the trash-cli package provides a command trash, which does what we want.



                        As Eliah Kagan makes clear in extensive comments, you can also make this recursive using find. In that case you can't use an alias, so the commands below assume you have installed trash-cli. I summarise Eliah's comments:



                        This command finds and displays all .bak files and symlinks anywhere in the current directory or its subdirectories or below.



                        find . -name '*.bak' -xtype f


                        To delete them, append an -exec with the trash command:



                        find . -name '*.bak' -xtype f -exec trash {} +


                        -xtype f selects files and symlinks to files, but not folders. To delete .bak folders too, remove that part, and use -execdir, which avoids cannot trash non-existent errors for .bak files inside .bak directories:



                        find . -name '*.bak' -execdir trash {} +






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Oct 20 '17 at 7:12









                        Zanna

                        48.9k13123234




                        48.9k13123234










                        answered Nov 15 '13 at 13:52









                        don.joey

                        16.9k126294




                        16.9k126294








                        • 3




                          "Don't use rm to delete things" is a controversial statement but I have to agree that it's often wiser to use something that will let you undo things.
                          – Oli
                          Nov 15 '13 at 15:12






                        • 2




                          The package trash-cli does the same thing as gvfs-trash without the dependency on gvfs.
                          – Flimm
                          Nov 20 '13 at 9:08










                        • I have edited it in the answer, next time feel free to do the edit yourself.
                          – don.joey
                          Nov 20 '13 at 10:06










                        • @don.joey This answer seems to say find . -name "*.bak" -type f displays what trash *.bak deletes. Is that really what you mean? You can move directories to the trash with trash or gvfs-trash, but trash *.bak will only moves files and directories whose names end with .bak and that reside immediately in the current directory. The shell expands *.bak, so trash *.bak won't affect .bak files in subdirectories not themselves named .bak.
                          – Eliah Kagan
                          Oct 14 '17 at 2:30








                        • 1




                          @don.joey Yes ls *.bak (which I think you mean) lists what trash *.bak trashes. find . -name '*.bak' -xtype f -exec trash {} + trashes all .bak files anywhere under .. It can't use an alias, so install trash-cli or write gvfs-trash instead. Here's an example. -xtype f selects files and symlinks to files, but not folders. To delete .bak folders too, use find . -name '*.bak' -execdir trash {} +, which avoids cannot trash non existent errors for .bak files inside .bak directories. Please feel free to use any of this in your answer.
                          – Eliah Kagan
                          Oct 14 '17 at 19:15














                        • 3




                          "Don't use rm to delete things" is a controversial statement but I have to agree that it's often wiser to use something that will let you undo things.
                          – Oli
                          Nov 15 '13 at 15:12






                        • 2




                          The package trash-cli does the same thing as gvfs-trash without the dependency on gvfs.
                          – Flimm
                          Nov 20 '13 at 9:08










                        • I have edited it in the answer, next time feel free to do the edit yourself.
                          – don.joey
                          Nov 20 '13 at 10:06










                        • @don.joey This answer seems to say find . -name "*.bak" -type f displays what trash *.bak deletes. Is that really what you mean? You can move directories to the trash with trash or gvfs-trash, but trash *.bak will only moves files and directories whose names end with .bak and that reside immediately in the current directory. The shell expands *.bak, so trash *.bak won't affect .bak files in subdirectories not themselves named .bak.
                          – Eliah Kagan
                          Oct 14 '17 at 2:30








                        • 1




                          @don.joey Yes ls *.bak (which I think you mean) lists what trash *.bak trashes. find . -name '*.bak' -xtype f -exec trash {} + trashes all .bak files anywhere under .. It can't use an alias, so install trash-cli or write gvfs-trash instead. Here's an example. -xtype f selects files and symlinks to files, but not folders. To delete .bak folders too, use find . -name '*.bak' -execdir trash {} +, which avoids cannot trash non existent errors for .bak files inside .bak directories. Please feel free to use any of this in your answer.
                          – Eliah Kagan
                          Oct 14 '17 at 19:15








                        3




                        3




                        "Don't use rm to delete things" is a controversial statement but I have to agree that it's often wiser to use something that will let you undo things.
                        – Oli
                        Nov 15 '13 at 15:12




                        "Don't use rm to delete things" is a controversial statement but I have to agree that it's often wiser to use something that will let you undo things.
                        – Oli
                        Nov 15 '13 at 15:12




                        2




                        2




                        The package trash-cli does the same thing as gvfs-trash without the dependency on gvfs.
                        – Flimm
                        Nov 20 '13 at 9:08




                        The package trash-cli does the same thing as gvfs-trash without the dependency on gvfs.
                        – Flimm
                        Nov 20 '13 at 9:08












                        I have edited it in the answer, next time feel free to do the edit yourself.
                        – don.joey
                        Nov 20 '13 at 10:06




                        I have edited it in the answer, next time feel free to do the edit yourself.
                        – don.joey
                        Nov 20 '13 at 10:06












                        @don.joey This answer seems to say find . -name "*.bak" -type f displays what trash *.bak deletes. Is that really what you mean? You can move directories to the trash with trash or gvfs-trash, but trash *.bak will only moves files and directories whose names end with .bak and that reside immediately in the current directory. The shell expands *.bak, so trash *.bak won't affect .bak files in subdirectories not themselves named .bak.
                        – Eliah Kagan
                        Oct 14 '17 at 2:30






                        @don.joey This answer seems to say find . -name "*.bak" -type f displays what trash *.bak deletes. Is that really what you mean? You can move directories to the trash with trash or gvfs-trash, but trash *.bak will only moves files and directories whose names end with .bak and that reside immediately in the current directory. The shell expands *.bak, so trash *.bak won't affect .bak files in subdirectories not themselves named .bak.
                        – Eliah Kagan
                        Oct 14 '17 at 2:30






                        1




                        1




                        @don.joey Yes ls *.bak (which I think you mean) lists what trash *.bak trashes. find . -name '*.bak' -xtype f -exec trash {} + trashes all .bak files anywhere under .. It can't use an alias, so install trash-cli or write gvfs-trash instead. Here's an example. -xtype f selects files and symlinks to files, but not folders. To delete .bak folders too, use find . -name '*.bak' -execdir trash {} +, which avoids cannot trash non existent errors for .bak files inside .bak directories. Please feel free to use any of this in your answer.
                        – Eliah Kagan
                        Oct 14 '17 at 19:15




                        @don.joey Yes ls *.bak (which I think you mean) lists what trash *.bak trashes. find . -name '*.bak' -xtype f -exec trash {} + trashes all .bak files anywhere under .. It can't use an alias, so install trash-cli or write gvfs-trash instead. Here's an example. -xtype f selects files and symlinks to files, but not folders. To delete .bak folders too, use find . -name '*.bak' -execdir trash {} +, which avoids cannot trash non existent errors for .bak files inside .bak directories. Please feel free to use any of this in your answer.
                        – Eliah Kagan
                        Oct 14 '17 at 19:15










                        up vote
                        3
                        down vote













                        If you want to delete all files of a certain type, but only 1 folder "deep" from the current folder:



                        find . -maxdepth 2 -name "*.log" -type f -delete


                        -maxdepth 2 because the current directory "." counts as the first folder.






                        share|improve this answer

























                          up vote
                          3
                          down vote













                          If you want to delete all files of a certain type, but only 1 folder "deep" from the current folder:



                          find . -maxdepth 2 -name "*.log" -type f -delete


                          -maxdepth 2 because the current directory "." counts as the first folder.






                          share|improve this answer























                            up vote
                            3
                            down vote










                            up vote
                            3
                            down vote









                            If you want to delete all files of a certain type, but only 1 folder "deep" from the current folder:



                            find . -maxdepth 2 -name "*.log" -type f -delete


                            -maxdepth 2 because the current directory "." counts as the first folder.






                            share|improve this answer












                            If you want to delete all files of a certain type, but only 1 folder "deep" from the current folder:



                            find . -maxdepth 2 -name "*.log" -type f -delete


                            -maxdepth 2 because the current directory "." counts as the first folder.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 26 '17 at 17:21









                            Hypocritus

                            493




                            493






















                                up vote
                                0
                                down vote













                                If you are inside a git repo, you can use:



                                git clean -fdx


                                This deletes untracked files and files in .gitignore.






                                share|improve this answer

























                                  up vote
                                  0
                                  down vote













                                  If you are inside a git repo, you can use:



                                  git clean -fdx


                                  This deletes untracked files and files in .gitignore.






                                  share|improve this answer























                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    If you are inside a git repo, you can use:



                                    git clean -fdx


                                    This deletes untracked files and files in .gitignore.






                                    share|improve this answer












                                    If you are inside a git repo, you can use:



                                    git clean -fdx


                                    This deletes untracked files and files in .gitignore.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Aug 9 at 7:38









                                    Vasantha Ganesh K

                                    94212




                                    94212






























                                         

                                        draft saved


                                        draft discarded



















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f377438%2fhow-can-i-recursively-delete-all-files-of-a-specific-extension-in-the-current-di%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

                                        數位音樂下載

                                        When can things happen in Etherscan, such as the picture below?

                                        格利澤436b