Removing all files containing a string in their name

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








up vote
0
down vote

favorite
1












Is there command line tool that removes everything that whereis returns (even if they are directories) but works even with partial names (e. g. if I give the string gmai it will also delete files that are named gmail).



So recursively look through all directories and delete the contents of those that contain a string in their name.



e. g. If the directories are mail/gmail/sentmail delete everything below gmail.










share|improve this question



















  • 5




    What whereis returns is very specific, and normally you wouldn't want to delete the files it lists. It's certainly possible to delete files based on all or part of their names, but I suggest you tell us exactly what you want to do rather than hint at it
    – Zanna
    Feb 20 at 17:15










  • @DavidFoerster I wanted what the answer is doing, except if I am doing this to delete, it might be faster not to look further inside directories that will be deleted
    – Nesa
    Feb 21 at 21:53






  • 1




    Unix file systems semantics don't allow the deletion of non-empty directories. Any tool that wants do successfully delete a directory needs to delete all of its children first.
    – David Foerster
    Feb 21 at 21:59














up vote
0
down vote

favorite
1












Is there command line tool that removes everything that whereis returns (even if they are directories) but works even with partial names (e. g. if I give the string gmai it will also delete files that are named gmail).



So recursively look through all directories and delete the contents of those that contain a string in their name.



e. g. If the directories are mail/gmail/sentmail delete everything below gmail.










share|improve this question



















  • 5




    What whereis returns is very specific, and normally you wouldn't want to delete the files it lists. It's certainly possible to delete files based on all or part of their names, but I suggest you tell us exactly what you want to do rather than hint at it
    – Zanna
    Feb 20 at 17:15










  • @DavidFoerster I wanted what the answer is doing, except if I am doing this to delete, it might be faster not to look further inside directories that will be deleted
    – Nesa
    Feb 21 at 21:53






  • 1




    Unix file systems semantics don't allow the deletion of non-empty directories. Any tool that wants do successfully delete a directory needs to delete all of its children first.
    – David Foerster
    Feb 21 at 21:59












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





Is there command line tool that removes everything that whereis returns (even if they are directories) but works even with partial names (e. g. if I give the string gmai it will also delete files that are named gmail).



So recursively look through all directories and delete the contents of those that contain a string in their name.



e. g. If the directories are mail/gmail/sentmail delete everything below gmail.










share|improve this question















Is there command line tool that removes everything that whereis returns (even if they are directories) but works even with partial names (e. g. if I give the string gmai it will also delete files that are named gmail).



So recursively look through all directories and delete the contents of those that contain a string in their name.



e. g. If the directories are mail/gmail/sentmail delete everything below gmail.







command-line






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 22 at 6:50









Zanna

48.2k13120228




48.2k13120228










asked Feb 20 at 17:03









Nesa

1053




1053







  • 5




    What whereis returns is very specific, and normally you wouldn't want to delete the files it lists. It's certainly possible to delete files based on all or part of their names, but I suggest you tell us exactly what you want to do rather than hint at it
    – Zanna
    Feb 20 at 17:15










  • @DavidFoerster I wanted what the answer is doing, except if I am doing this to delete, it might be faster not to look further inside directories that will be deleted
    – Nesa
    Feb 21 at 21:53






  • 1




    Unix file systems semantics don't allow the deletion of non-empty directories. Any tool that wants do successfully delete a directory needs to delete all of its children first.
    – David Foerster
    Feb 21 at 21:59












  • 5




    What whereis returns is very specific, and normally you wouldn't want to delete the files it lists. It's certainly possible to delete files based on all or part of their names, but I suggest you tell us exactly what you want to do rather than hint at it
    – Zanna
    Feb 20 at 17:15










  • @DavidFoerster I wanted what the answer is doing, except if I am doing this to delete, it might be faster not to look further inside directories that will be deleted
    – Nesa
    Feb 21 at 21:53






  • 1




    Unix file systems semantics don't allow the deletion of non-empty directories. Any tool that wants do successfully delete a directory needs to delete all of its children first.
    – David Foerster
    Feb 21 at 21:59







5




5




What whereis returns is very specific, and normally you wouldn't want to delete the files it lists. It's certainly possible to delete files based on all or part of their names, but I suggest you tell us exactly what you want to do rather than hint at it
– Zanna
Feb 20 at 17:15




What whereis returns is very specific, and normally you wouldn't want to delete the files it lists. It's certainly possible to delete files based on all or part of their names, but I suggest you tell us exactly what you want to do rather than hint at it
– Zanna
Feb 20 at 17:15












@DavidFoerster I wanted what the answer is doing, except if I am doing this to delete, it might be faster not to look further inside directories that will be deleted
– Nesa
Feb 21 at 21:53




@DavidFoerster I wanted what the answer is doing, except if I am doing this to delete, it might be faster not to look further inside directories that will be deleted
– Nesa
Feb 21 at 21:53




1




1




Unix file systems semantics don't allow the deletion of non-empty directories. Any tool that wants do successfully delete a directory needs to delete all of its children first.
– David Foerster
Feb 21 at 21:59




Unix file systems semantics don't allow the deletion of non-empty directories. Any tool that wants do successfully delete a directory needs to delete all of its children first.
– David Foerster
Feb 21 at 21:59










2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










If you really want to do this, you can use this find command, but you should test before without the switch -delete to test if it's what's you expect :



find ./mail -depth -path '*gmai*' -delete


It's recursive in all sub-dirs



With the help of comment from @David Foerster and my original command






share|improve this answer





























    up vote
    2
    down vote













    whereis(1) doesn't sound suitable to your purpose since it's no general purpose path name search tool.




    Without a search index



    As Gilles noted in his answer you can use find(1) for this job but I want to make a small yet significant improvement1:





    find [PATH...] -depth -path '*gmai*' -print # -delete


    Explanation:



    • find [PATH...] goes through all files and subdirectories below each PATH or the current working directories, if no path was given, and prints all matches (by default).


    • -path PATTERN matches full paths against PATTERN using globbing if any, so *gmai* matches all path names with an infix gmai.


    • -delete deletes all matched paths (and overrides the default action to print them).


    • -depth instructs find to match children before parents, i. e. directory entries before the parent directories themselves. It's necessary to delete children before their parents because in Unix' file system semantics only empty directories can be deleted.


    • -print prints all matched paths. Use this to check the result before the actual deletion. Comment in the -delete command (by removing the # in front of it) to actually delete them.



    With a search index



    If all the locations that you intend to delete appear in the mlocate.db(5) search index you can use it for faster searches:



    locate '/your/parent/path/*gmai*'


    searches all (indexed) paths that start with /your/parent/path/ and have an infix gmai in their remainder. This command only lists search results.



    To delete the results use:



    locate -0 '/your/parent/path/*gmai*' | xargs -r -0 -- rm -rf --


    Explanation:



    • | redirects the output of the left-side command to the input of the right-side command.


    • xargs collects "items" from its input, appends them to a given command and runs that command.


    • -r makes xargs not run the command if no input item occurs.


    • rm -rf removes files and directories recursively


    • -0 tells locate to delimit matches by null-bytes and xargs to accept null-separated input items. This avoids issues with path names that contain white-space and, more specifically, line break characters.



    1 that he incorporated in his answer before the question was reopened and allowed me to answer.






    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: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      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%2f1008103%2fremoving-all-files-containing-a-string-in-their-name%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote



      accepted










      If you really want to do this, you can use this find command, but you should test before without the switch -delete to test if it's what's you expect :



      find ./mail -depth -path '*gmai*' -delete


      It's recursive in all sub-dirs



      With the help of comment from @David Foerster and my original command






      share|improve this answer


























        up vote
        4
        down vote



        accepted










        If you really want to do this, you can use this find command, but you should test before without the switch -delete to test if it's what's you expect :



        find ./mail -depth -path '*gmai*' -delete


        It's recursive in all sub-dirs



        With the help of comment from @David Foerster and my original command






        share|improve this answer
























          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          If you really want to do this, you can use this find command, but you should test before without the switch -delete to test if it's what's you expect :



          find ./mail -depth -path '*gmai*' -delete


          It's recursive in all sub-dirs



          With the help of comment from @David Foerster and my original command






          share|improve this answer














          If you really want to do this, you can use this find command, but you should test before without the switch -delete to test if it's what's you expect :



          find ./mail -depth -path '*gmai*' -delete


          It's recursive in all sub-dirs



          With the help of comment from @David Foerster and my original command







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 21 at 22:22

























          answered Feb 20 at 23:47









          Gilles Quenot

          1,712188




          1,712188






















              up vote
              2
              down vote













              whereis(1) doesn't sound suitable to your purpose since it's no general purpose path name search tool.




              Without a search index



              As Gilles noted in his answer you can use find(1) for this job but I want to make a small yet significant improvement1:





              find [PATH...] -depth -path '*gmai*' -print # -delete


              Explanation:



              • find [PATH...] goes through all files and subdirectories below each PATH or the current working directories, if no path was given, and prints all matches (by default).


              • -path PATTERN matches full paths against PATTERN using globbing if any, so *gmai* matches all path names with an infix gmai.


              • -delete deletes all matched paths (and overrides the default action to print them).


              • -depth instructs find to match children before parents, i. e. directory entries before the parent directories themselves. It's necessary to delete children before their parents because in Unix' file system semantics only empty directories can be deleted.


              • -print prints all matched paths. Use this to check the result before the actual deletion. Comment in the -delete command (by removing the # in front of it) to actually delete them.



              With a search index



              If all the locations that you intend to delete appear in the mlocate.db(5) search index you can use it for faster searches:



              locate '/your/parent/path/*gmai*'


              searches all (indexed) paths that start with /your/parent/path/ and have an infix gmai in their remainder. This command only lists search results.



              To delete the results use:



              locate -0 '/your/parent/path/*gmai*' | xargs -r -0 -- rm -rf --


              Explanation:



              • | redirects the output of the left-side command to the input of the right-side command.


              • xargs collects "items" from its input, appends them to a given command and runs that command.


              • -r makes xargs not run the command if no input item occurs.


              • rm -rf removes files and directories recursively


              • -0 tells locate to delimit matches by null-bytes and xargs to accept null-separated input items. This avoids issues with path names that contain white-space and, more specifically, line break characters.



              1 that he incorporated in his answer before the question was reopened and allowed me to answer.






              share|improve this answer


























                up vote
                2
                down vote













                whereis(1) doesn't sound suitable to your purpose since it's no general purpose path name search tool.




                Without a search index



                As Gilles noted in his answer you can use find(1) for this job but I want to make a small yet significant improvement1:





                find [PATH...] -depth -path '*gmai*' -print # -delete


                Explanation:



                • find [PATH...] goes through all files and subdirectories below each PATH or the current working directories, if no path was given, and prints all matches (by default).


                • -path PATTERN matches full paths against PATTERN using globbing if any, so *gmai* matches all path names with an infix gmai.


                • -delete deletes all matched paths (and overrides the default action to print them).


                • -depth instructs find to match children before parents, i. e. directory entries before the parent directories themselves. It's necessary to delete children before their parents because in Unix' file system semantics only empty directories can be deleted.


                • -print prints all matched paths. Use this to check the result before the actual deletion. Comment in the -delete command (by removing the # in front of it) to actually delete them.



                With a search index



                If all the locations that you intend to delete appear in the mlocate.db(5) search index you can use it for faster searches:



                locate '/your/parent/path/*gmai*'


                searches all (indexed) paths that start with /your/parent/path/ and have an infix gmai in their remainder. This command only lists search results.



                To delete the results use:



                locate -0 '/your/parent/path/*gmai*' | xargs -r -0 -- rm -rf --


                Explanation:



                • | redirects the output of the left-side command to the input of the right-side command.


                • xargs collects "items" from its input, appends them to a given command and runs that command.


                • -r makes xargs not run the command if no input item occurs.


                • rm -rf removes files and directories recursively


                • -0 tells locate to delimit matches by null-bytes and xargs to accept null-separated input items. This avoids issues with path names that contain white-space and, more specifically, line break characters.



                1 that he incorporated in his answer before the question was reopened and allowed me to answer.






                share|improve this answer
























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  whereis(1) doesn't sound suitable to your purpose since it's no general purpose path name search tool.




                  Without a search index



                  As Gilles noted in his answer you can use find(1) for this job but I want to make a small yet significant improvement1:





                  find [PATH...] -depth -path '*gmai*' -print # -delete


                  Explanation:



                  • find [PATH...] goes through all files and subdirectories below each PATH or the current working directories, if no path was given, and prints all matches (by default).


                  • -path PATTERN matches full paths against PATTERN using globbing if any, so *gmai* matches all path names with an infix gmai.


                  • -delete deletes all matched paths (and overrides the default action to print them).


                  • -depth instructs find to match children before parents, i. e. directory entries before the parent directories themselves. It's necessary to delete children before their parents because in Unix' file system semantics only empty directories can be deleted.


                  • -print prints all matched paths. Use this to check the result before the actual deletion. Comment in the -delete command (by removing the # in front of it) to actually delete them.



                  With a search index



                  If all the locations that you intend to delete appear in the mlocate.db(5) search index you can use it for faster searches:



                  locate '/your/parent/path/*gmai*'


                  searches all (indexed) paths that start with /your/parent/path/ and have an infix gmai in their remainder. This command only lists search results.



                  To delete the results use:



                  locate -0 '/your/parent/path/*gmai*' | xargs -r -0 -- rm -rf --


                  Explanation:



                  • | redirects the output of the left-side command to the input of the right-side command.


                  • xargs collects "items" from its input, appends them to a given command and runs that command.


                  • -r makes xargs not run the command if no input item occurs.


                  • rm -rf removes files and directories recursively


                  • -0 tells locate to delimit matches by null-bytes and xargs to accept null-separated input items. This avoids issues with path names that contain white-space and, more specifically, line break characters.



                  1 that he incorporated in his answer before the question was reopened and allowed me to answer.






                  share|improve this answer














                  whereis(1) doesn't sound suitable to your purpose since it's no general purpose path name search tool.




                  Without a search index



                  As Gilles noted in his answer you can use find(1) for this job but I want to make a small yet significant improvement1:





                  find [PATH...] -depth -path '*gmai*' -print # -delete


                  Explanation:



                  • find [PATH...] goes through all files and subdirectories below each PATH or the current working directories, if no path was given, and prints all matches (by default).


                  • -path PATTERN matches full paths against PATTERN using globbing if any, so *gmai* matches all path names with an infix gmai.


                  • -delete deletes all matched paths (and overrides the default action to print them).


                  • -depth instructs find to match children before parents, i. e. directory entries before the parent directories themselves. It's necessary to delete children before their parents because in Unix' file system semantics only empty directories can be deleted.


                  • -print prints all matched paths. Use this to check the result before the actual deletion. Comment in the -delete command (by removing the # in front of it) to actually delete them.



                  With a search index



                  If all the locations that you intend to delete appear in the mlocate.db(5) search index you can use it for faster searches:



                  locate '/your/parent/path/*gmai*'


                  searches all (indexed) paths that start with /your/parent/path/ and have an infix gmai in their remainder. This command only lists search results.



                  To delete the results use:



                  locate -0 '/your/parent/path/*gmai*' | xargs -r -0 -- rm -rf --


                  Explanation:



                  • | redirects the output of the left-side command to the input of the right-side command.


                  • xargs collects "items" from its input, appends them to a given command and runs that command.


                  • -r makes xargs not run the command if no input item occurs.


                  • rm -rf removes files and directories recursively


                  • -0 tells locate to delimit matches by null-bytes and xargs to accept null-separated input items. This avoids issues with path names that contain white-space and, more specifically, line break characters.



                  1 that he incorporated in his answer before the question was reopened and allowed me to answer.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 22 at 20:35

























                  answered Feb 22 at 13:22









                  David Foerster

                  26.5k1362106




                  26.5k1362106



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1008103%2fremoving-all-files-containing-a-string-in-their-name%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      pylint3 and pip3 broken

                      Missing snmpget and snmpwalk

                      How to enroll fingerprints to Ubuntu 17.10 with VFS491