Deleting files using pattern matching for files containing spaces and asterisks

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











up vote
1
down vote

favorite












Due to a program that wasn't functioning well, I have many files in my home directory starting with a dot and containing strange symbols among which are spaces and asterisks. I want to delete these files, but am having trouble doing so. This problem is similar to Rename file containing spaces and punctuation marks,
but much worse. There are way too many files to remove by hand so I will have to use pattern matching.



Most files start with .txt} and contain 18Jun22h (and no files in the directory that I want to keep contain either of these strings). How do I remove all files containing this string given that the files in the directory are certainly not nice unix name? Instead, they contain dots spaces asterisks and brackets and dashes. My initial attempts are failing presumably due to the presence of these symbols in these names.



Any method that deletes them one by one would be completely useless to me due to the quantity of files.




Yes I know it is better to not have files not living up to the unix standard. This is a code I also live by. But a script did not work as it should.




As a second thought, it might be easier to delete all files produced (in the specific directory) between certain times. So alternatively help with this is appreciated.




System: Fedora 27



I had tried ~/*18Jun22h* but thought that the error saying the argument was too long had to do with the *'s in the filenames expanding and doing something strange. Instead it is just because there are too many files matching this pattern (right?). (I did not know that there is a max on the number of files rm can handle).







share|improve this question





















  • It's difficult to suggest anything without knowing what the files are or if they all contain a specific pattern.
    – Nasir Riley
    Jun 25 at 18:07










  • Show us what you tried.
    – ctrl-alt-delor
    Jun 25 at 18:09










  • Do the file names contain 18Jun22h or is that in the contents of the file?
    – Eric Renouf
    Jun 25 at 18:14










  • is this on a GNU/Linux system, or a different OS?
    – Jeff Schaller
    Jun 25 at 18:40














up vote
1
down vote

favorite












Due to a program that wasn't functioning well, I have many files in my home directory starting with a dot and containing strange symbols among which are spaces and asterisks. I want to delete these files, but am having trouble doing so. This problem is similar to Rename file containing spaces and punctuation marks,
but much worse. There are way too many files to remove by hand so I will have to use pattern matching.



Most files start with .txt} and contain 18Jun22h (and no files in the directory that I want to keep contain either of these strings). How do I remove all files containing this string given that the files in the directory are certainly not nice unix name? Instead, they contain dots spaces asterisks and brackets and dashes. My initial attempts are failing presumably due to the presence of these symbols in these names.



Any method that deletes them one by one would be completely useless to me due to the quantity of files.




Yes I know it is better to not have files not living up to the unix standard. This is a code I also live by. But a script did not work as it should.




As a second thought, it might be easier to delete all files produced (in the specific directory) between certain times. So alternatively help with this is appreciated.




System: Fedora 27



I had tried ~/*18Jun22h* but thought that the error saying the argument was too long had to do with the *'s in the filenames expanding and doing something strange. Instead it is just because there are too many files matching this pattern (right?). (I did not know that there is a max on the number of files rm can handle).







share|improve this question





















  • It's difficult to suggest anything without knowing what the files are or if they all contain a specific pattern.
    – Nasir Riley
    Jun 25 at 18:07










  • Show us what you tried.
    – ctrl-alt-delor
    Jun 25 at 18:09










  • Do the file names contain 18Jun22h or is that in the contents of the file?
    – Eric Renouf
    Jun 25 at 18:14










  • is this on a GNU/Linux system, or a different OS?
    – Jeff Schaller
    Jun 25 at 18:40












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Due to a program that wasn't functioning well, I have many files in my home directory starting with a dot and containing strange symbols among which are spaces and asterisks. I want to delete these files, but am having trouble doing so. This problem is similar to Rename file containing spaces and punctuation marks,
but much worse. There are way too many files to remove by hand so I will have to use pattern matching.



Most files start with .txt} and contain 18Jun22h (and no files in the directory that I want to keep contain either of these strings). How do I remove all files containing this string given that the files in the directory are certainly not nice unix name? Instead, they contain dots spaces asterisks and brackets and dashes. My initial attempts are failing presumably due to the presence of these symbols in these names.



Any method that deletes them one by one would be completely useless to me due to the quantity of files.




Yes I know it is better to not have files not living up to the unix standard. This is a code I also live by. But a script did not work as it should.




As a second thought, it might be easier to delete all files produced (in the specific directory) between certain times. So alternatively help with this is appreciated.




System: Fedora 27



I had tried ~/*18Jun22h* but thought that the error saying the argument was too long had to do with the *'s in the filenames expanding and doing something strange. Instead it is just because there are too many files matching this pattern (right?). (I did not know that there is a max on the number of files rm can handle).







share|improve this question













Due to a program that wasn't functioning well, I have many files in my home directory starting with a dot and containing strange symbols among which are spaces and asterisks. I want to delete these files, but am having trouble doing so. This problem is similar to Rename file containing spaces and punctuation marks,
but much worse. There are way too many files to remove by hand so I will have to use pattern matching.



Most files start with .txt} and contain 18Jun22h (and no files in the directory that I want to keep contain either of these strings). How do I remove all files containing this string given that the files in the directory are certainly not nice unix name? Instead, they contain dots spaces asterisks and brackets and dashes. My initial attempts are failing presumably due to the presence of these symbols in these names.



Any method that deletes them one by one would be completely useless to me due to the quantity of files.




Yes I know it is better to not have files not living up to the unix standard. This is a code I also live by. But a script did not work as it should.




As a second thought, it might be easier to delete all files produced (in the specific directory) between certain times. So alternatively help with this is appreciated.




System: Fedora 27



I had tried ~/*18Jun22h* but thought that the error saying the argument was too long had to do with the *'s in the filenames expanding and doing something strange. Instead it is just because there are too many files matching this pattern (right?). (I did not know that there is a max on the number of files rm can handle).









share|improve this question












share|improve this question




share|improve this question








edited Jun 26 at 8:45
























asked Jun 25 at 18:00









Kvothe

1186




1186











  • It's difficult to suggest anything without knowing what the files are or if they all contain a specific pattern.
    – Nasir Riley
    Jun 25 at 18:07










  • Show us what you tried.
    – ctrl-alt-delor
    Jun 25 at 18:09










  • Do the file names contain 18Jun22h or is that in the contents of the file?
    – Eric Renouf
    Jun 25 at 18:14










  • is this on a GNU/Linux system, or a different OS?
    – Jeff Schaller
    Jun 25 at 18:40
















  • It's difficult to suggest anything without knowing what the files are or if they all contain a specific pattern.
    – Nasir Riley
    Jun 25 at 18:07










  • Show us what you tried.
    – ctrl-alt-delor
    Jun 25 at 18:09










  • Do the file names contain 18Jun22h or is that in the contents of the file?
    – Eric Renouf
    Jun 25 at 18:14










  • is this on a GNU/Linux system, or a different OS?
    – Jeff Schaller
    Jun 25 at 18:40















It's difficult to suggest anything without knowing what the files are or if they all contain a specific pattern.
– Nasir Riley
Jun 25 at 18:07




It's difficult to suggest anything without knowing what the files are or if they all contain a specific pattern.
– Nasir Riley
Jun 25 at 18:07












Show us what you tried.
– ctrl-alt-delor
Jun 25 at 18:09




Show us what you tried.
– ctrl-alt-delor
Jun 25 at 18:09












Do the file names contain 18Jun22h or is that in the contents of the file?
– Eric Renouf
Jun 25 at 18:14




Do the file names contain 18Jun22h or is that in the contents of the file?
– Eric Renouf
Jun 25 at 18:14












is this on a GNU/Linux system, or a different OS?
– Jeff Schaller
Jun 25 at 18:40




is this on a GNU/Linux system, or a different OS?
– Jeff Schaller
Jun 25 at 18:40










5 Answers
5






active

oldest

votes

















up vote
3
down vote



accepted










To find files in the home directory (only) that contains at least one space:



find "$HOME" -maxdepth 1 -type f -name '*[ ]*'


... that contain at least one *:



find "$HOME" -maxdepth 1 -type f -name '*[*]*'


... that contain at least [ or ]:



find "$HOME" -maxdepth 1 -type f -name '*[[]]*'


... that contain one of any of the above:



find "$HOME" -maxdepth 1 -type f -name '*[ *[]]*'


To delete these files, add either -delete to the end of the command, or -exec rm +.




On systems where find does not support -maxdepth 1, running from the home directory:



find . -type d ! -name . -prune -o -type f -name '*[ *[]]*' -print





share|improve this answer























  • @JeffSchaller Ah, I missed the bit about the home directory. You're right.
    – Kusalananda
    Jun 25 at 18:40










  • -name '*[* ]*' would probably be more portable. No need for -type d, -prune is harmless on non-directories: find . ! -name . -prune -type f -name '*[* ]*'
    – Stéphane Chazelas
    Jun 26 at 9:00










  • You may need LC_ALL=C as without it, that may fail to match on file names that contain bytes not forming valid characters in the locale's charset.
    – Stéphane Chazelas
    Jun 26 at 9:02

















up vote
1
down vote













Since none of .txt} is special to the shell (there's no opening curly brace), simply:



rm ~/.txt}* ~/*18Jun22h*


or if there are too many, split it up:



rm ~/.txt}*
rm ~/*18Jun22h*





share|improve this answer





















  • These didn't work. The first line (second part) says the argument is too long. The second part actually does not seem to find the files at all. It gives: "rm: cannot remove '/home/name/*18Jun22h*': No such file or directory". Even though with ls -a it is clear that the files containing this do exist (should I add an option to delete hidden files (note the files start with a dot).
    – Kvothe
    Jun 26 at 8:35











  • Depends on the shell. rm ~/.txt}* would be a syntax error in zsh (except in sh emulation), fish and rc and derivatives.
    – Stéphane Chazelas
    Jun 26 at 8:53

















up vote
0
down vote













You can come in and out of quotations.



E.g. to remove A file starting with *, then do



rm "*"*



Practice with echo first echo "*"*






share|improve this answer




























    up vote
    0
    down vote













    You can get the filenames for files which contain a given expression with grep -l, for example grep -l '18Jun22h' *. You can use --null to pass a null character between the files, and send that to xargs:



    $ grep -l '18Jun22h' * --null | xargs -0 rm --


    I would recommend doing a sanity check first though, as with this 'create and then remove the files' demonstration:



    $ for file in 'test file' 'file?name' 'file*name'; do echo foo > "$file"; done; ls -1
    file*name
    file?name
    test file
    $ grep -l foo * --null | xargs -L1 -0 echo
    file*name
    file?name
    test file
    $ grep -l foo * --null | xargs -L1 -0 rm --





    share|improve this answer






























      up vote
      0
      down vote













      Way 1: using ./



      rm -f ./'file name' and rm -f ./'file*name'



      Way 2: using inode:



      Get the inode from ls



      ls -i "file name"



      then



      find . -inum "numberoofinode" -exec rm ;






      share|improve this answer





















      • Except for the note from the OP: “Any method that deletes them one by one would be completely useless to me due to the quantity of files.”
        – Jeff Schaller
        Jun 25 at 19:49











      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      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%2funix.stackexchange.com%2fquestions%2f451810%2fdeleting-files-using-pattern-matching-for-files-containing-spaces-and-asterisks%23new-answer', 'question_page');

      );

      Post as a guest






























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      To find files in the home directory (only) that contains at least one space:



      find "$HOME" -maxdepth 1 -type f -name '*[ ]*'


      ... that contain at least one *:



      find "$HOME" -maxdepth 1 -type f -name '*[*]*'


      ... that contain at least [ or ]:



      find "$HOME" -maxdepth 1 -type f -name '*[[]]*'


      ... that contain one of any of the above:



      find "$HOME" -maxdepth 1 -type f -name '*[ *[]]*'


      To delete these files, add either -delete to the end of the command, or -exec rm +.




      On systems where find does not support -maxdepth 1, running from the home directory:



      find . -type d ! -name . -prune -o -type f -name '*[ *[]]*' -print





      share|improve this answer























      • @JeffSchaller Ah, I missed the bit about the home directory. You're right.
        – Kusalananda
        Jun 25 at 18:40










      • -name '*[* ]*' would probably be more portable. No need for -type d, -prune is harmless on non-directories: find . ! -name . -prune -type f -name '*[* ]*'
        – Stéphane Chazelas
        Jun 26 at 9:00










      • You may need LC_ALL=C as without it, that may fail to match on file names that contain bytes not forming valid characters in the locale's charset.
        – Stéphane Chazelas
        Jun 26 at 9:02














      up vote
      3
      down vote



      accepted










      To find files in the home directory (only) that contains at least one space:



      find "$HOME" -maxdepth 1 -type f -name '*[ ]*'


      ... that contain at least one *:



      find "$HOME" -maxdepth 1 -type f -name '*[*]*'


      ... that contain at least [ or ]:



      find "$HOME" -maxdepth 1 -type f -name '*[[]]*'


      ... that contain one of any of the above:



      find "$HOME" -maxdepth 1 -type f -name '*[ *[]]*'


      To delete these files, add either -delete to the end of the command, or -exec rm +.




      On systems where find does not support -maxdepth 1, running from the home directory:



      find . -type d ! -name . -prune -o -type f -name '*[ *[]]*' -print





      share|improve this answer























      • @JeffSchaller Ah, I missed the bit about the home directory. You're right.
        – Kusalananda
        Jun 25 at 18:40










      • -name '*[* ]*' would probably be more portable. No need for -type d, -prune is harmless on non-directories: find . ! -name . -prune -type f -name '*[* ]*'
        – Stéphane Chazelas
        Jun 26 at 9:00










      • You may need LC_ALL=C as without it, that may fail to match on file names that contain bytes not forming valid characters in the locale's charset.
        – Stéphane Chazelas
        Jun 26 at 9:02












      up vote
      3
      down vote



      accepted







      up vote
      3
      down vote



      accepted






      To find files in the home directory (only) that contains at least one space:



      find "$HOME" -maxdepth 1 -type f -name '*[ ]*'


      ... that contain at least one *:



      find "$HOME" -maxdepth 1 -type f -name '*[*]*'


      ... that contain at least [ or ]:



      find "$HOME" -maxdepth 1 -type f -name '*[[]]*'


      ... that contain one of any of the above:



      find "$HOME" -maxdepth 1 -type f -name '*[ *[]]*'


      To delete these files, add either -delete to the end of the command, or -exec rm +.




      On systems where find does not support -maxdepth 1, running from the home directory:



      find . -type d ! -name . -prune -o -type f -name '*[ *[]]*' -print





      share|improve this answer















      To find files in the home directory (only) that contains at least one space:



      find "$HOME" -maxdepth 1 -type f -name '*[ ]*'


      ... that contain at least one *:



      find "$HOME" -maxdepth 1 -type f -name '*[*]*'


      ... that contain at least [ or ]:



      find "$HOME" -maxdepth 1 -type f -name '*[[]]*'


      ... that contain one of any of the above:



      find "$HOME" -maxdepth 1 -type f -name '*[ *[]]*'


      To delete these files, add either -delete to the end of the command, or -exec rm +.




      On systems where find does not support -maxdepth 1, running from the home directory:



      find . -type d ! -name . -prune -o -type f -name '*[ *[]]*' -print






      share|improve this answer















      share|improve this answer



      share|improve this answer








      edited Jun 25 at 18:50


























      answered Jun 25 at 18:20









      Kusalananda

      101k13199312




      101k13199312











      • @JeffSchaller Ah, I missed the bit about the home directory. You're right.
        – Kusalananda
        Jun 25 at 18:40










      • -name '*[* ]*' would probably be more portable. No need for -type d, -prune is harmless on non-directories: find . ! -name . -prune -type f -name '*[* ]*'
        – Stéphane Chazelas
        Jun 26 at 9:00










      • You may need LC_ALL=C as without it, that may fail to match on file names that contain bytes not forming valid characters in the locale's charset.
        – Stéphane Chazelas
        Jun 26 at 9:02
















      • @JeffSchaller Ah, I missed the bit about the home directory. You're right.
        – Kusalananda
        Jun 25 at 18:40










      • -name '*[* ]*' would probably be more portable. No need for -type d, -prune is harmless on non-directories: find . ! -name . -prune -type f -name '*[* ]*'
        – Stéphane Chazelas
        Jun 26 at 9:00










      • You may need LC_ALL=C as without it, that may fail to match on file names that contain bytes not forming valid characters in the locale's charset.
        – Stéphane Chazelas
        Jun 26 at 9:02















      @JeffSchaller Ah, I missed the bit about the home directory. You're right.
      – Kusalananda
      Jun 25 at 18:40




      @JeffSchaller Ah, I missed the bit about the home directory. You're right.
      – Kusalananda
      Jun 25 at 18:40












      -name '*[* ]*' would probably be more portable. No need for -type d, -prune is harmless on non-directories: find . ! -name . -prune -type f -name '*[* ]*'
      – Stéphane Chazelas
      Jun 26 at 9:00




      -name '*[* ]*' would probably be more portable. No need for -type d, -prune is harmless on non-directories: find . ! -name . -prune -type f -name '*[* ]*'
      – Stéphane Chazelas
      Jun 26 at 9:00












      You may need LC_ALL=C as without it, that may fail to match on file names that contain bytes not forming valid characters in the locale's charset.
      – Stéphane Chazelas
      Jun 26 at 9:02




      You may need LC_ALL=C as without it, that may fail to match on file names that contain bytes not forming valid characters in the locale's charset.
      – Stéphane Chazelas
      Jun 26 at 9:02












      up vote
      1
      down vote













      Since none of .txt} is special to the shell (there's no opening curly brace), simply:



      rm ~/.txt}* ~/*18Jun22h*


      or if there are too many, split it up:



      rm ~/.txt}*
      rm ~/*18Jun22h*





      share|improve this answer





















      • These didn't work. The first line (second part) says the argument is too long. The second part actually does not seem to find the files at all. It gives: "rm: cannot remove '/home/name/*18Jun22h*': No such file or directory". Even though with ls -a it is clear that the files containing this do exist (should I add an option to delete hidden files (note the files start with a dot).
        – Kvothe
        Jun 26 at 8:35











      • Depends on the shell. rm ~/.txt}* would be a syntax error in zsh (except in sh emulation), fish and rc and derivatives.
        – Stéphane Chazelas
        Jun 26 at 8:53














      up vote
      1
      down vote













      Since none of .txt} is special to the shell (there's no opening curly brace), simply:



      rm ~/.txt}* ~/*18Jun22h*


      or if there are too many, split it up:



      rm ~/.txt}*
      rm ~/*18Jun22h*





      share|improve this answer





















      • These didn't work. The first line (second part) says the argument is too long. The second part actually does not seem to find the files at all. It gives: "rm: cannot remove '/home/name/*18Jun22h*': No such file or directory". Even though with ls -a it is clear that the files containing this do exist (should I add an option to delete hidden files (note the files start with a dot).
        – Kvothe
        Jun 26 at 8:35











      • Depends on the shell. rm ~/.txt}* would be a syntax error in zsh (except in sh emulation), fish and rc and derivatives.
        – Stéphane Chazelas
        Jun 26 at 8:53












      up vote
      1
      down vote










      up vote
      1
      down vote









      Since none of .txt} is special to the shell (there's no opening curly brace), simply:



      rm ~/.txt}* ~/*18Jun22h*


      or if there are too many, split it up:



      rm ~/.txt}*
      rm ~/*18Jun22h*





      share|improve this answer













      Since none of .txt} is special to the shell (there's no opening curly brace), simply:



      rm ~/.txt}* ~/*18Jun22h*


      or if there are too many, split it up:



      rm ~/.txt}*
      rm ~/*18Jun22h*






      share|improve this answer













      share|improve this answer



      share|improve this answer











      answered Jun 25 at 18:39









      Jeff Schaller

      30.8k846104




      30.8k846104











      • These didn't work. The first line (second part) says the argument is too long. The second part actually does not seem to find the files at all. It gives: "rm: cannot remove '/home/name/*18Jun22h*': No such file or directory". Even though with ls -a it is clear that the files containing this do exist (should I add an option to delete hidden files (note the files start with a dot).
        – Kvothe
        Jun 26 at 8:35











      • Depends on the shell. rm ~/.txt}* would be a syntax error in zsh (except in sh emulation), fish and rc and derivatives.
        – Stéphane Chazelas
        Jun 26 at 8:53
















      • These didn't work. The first line (second part) says the argument is too long. The second part actually does not seem to find the files at all. It gives: "rm: cannot remove '/home/name/*18Jun22h*': No such file or directory". Even though with ls -a it is clear that the files containing this do exist (should I add an option to delete hidden files (note the files start with a dot).
        – Kvothe
        Jun 26 at 8:35











      • Depends on the shell. rm ~/.txt}* would be a syntax error in zsh (except in sh emulation), fish and rc and derivatives.
        – Stéphane Chazelas
        Jun 26 at 8:53















      These didn't work. The first line (second part) says the argument is too long. The second part actually does not seem to find the files at all. It gives: "rm: cannot remove '/home/name/*18Jun22h*': No such file or directory". Even though with ls -a it is clear that the files containing this do exist (should I add an option to delete hidden files (note the files start with a dot).
      – Kvothe
      Jun 26 at 8:35





      These didn't work. The first line (second part) says the argument is too long. The second part actually does not seem to find the files at all. It gives: "rm: cannot remove '/home/name/*18Jun22h*': No such file or directory". Even though with ls -a it is clear that the files containing this do exist (should I add an option to delete hidden files (note the files start with a dot).
      – Kvothe
      Jun 26 at 8:35













      Depends on the shell. rm ~/.txt}* would be a syntax error in zsh (except in sh emulation), fish and rc and derivatives.
      – Stéphane Chazelas
      Jun 26 at 8:53




      Depends on the shell. rm ~/.txt}* would be a syntax error in zsh (except in sh emulation), fish and rc and derivatives.
      – Stéphane Chazelas
      Jun 26 at 8:53










      up vote
      0
      down vote













      You can come in and out of quotations.



      E.g. to remove A file starting with *, then do



      rm "*"*



      Practice with echo first echo "*"*






      share|improve this answer

























        up vote
        0
        down vote













        You can come in and out of quotations.



        E.g. to remove A file starting with *, then do



        rm "*"*



        Practice with echo first echo "*"*






        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          You can come in and out of quotations.



          E.g. to remove A file starting with *, then do



          rm "*"*



          Practice with echo first echo "*"*






          share|improve this answer













          You can come in and out of quotations.



          E.g. to remove A file starting with *, then do



          rm "*"*



          Practice with echo first echo "*"*







          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Jun 25 at 18:07









          ctrl-alt-delor

          8,70331947




          8,70331947




















              up vote
              0
              down vote













              You can get the filenames for files which contain a given expression with grep -l, for example grep -l '18Jun22h' *. You can use --null to pass a null character between the files, and send that to xargs:



              $ grep -l '18Jun22h' * --null | xargs -0 rm --


              I would recommend doing a sanity check first though, as with this 'create and then remove the files' demonstration:



              $ for file in 'test file' 'file?name' 'file*name'; do echo foo > "$file"; done; ls -1
              file*name
              file?name
              test file
              $ grep -l foo * --null | xargs -L1 -0 echo
              file*name
              file?name
              test file
              $ grep -l foo * --null | xargs -L1 -0 rm --





              share|improve this answer



























                up vote
                0
                down vote













                You can get the filenames for files which contain a given expression with grep -l, for example grep -l '18Jun22h' *. You can use --null to pass a null character between the files, and send that to xargs:



                $ grep -l '18Jun22h' * --null | xargs -0 rm --


                I would recommend doing a sanity check first though, as with this 'create and then remove the files' demonstration:



                $ for file in 'test file' 'file?name' 'file*name'; do echo foo > "$file"; done; ls -1
                file*name
                file?name
                test file
                $ grep -l foo * --null | xargs -L1 -0 echo
                file*name
                file?name
                test file
                $ grep -l foo * --null | xargs -L1 -0 rm --





                share|improve this answer

























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  You can get the filenames for files which contain a given expression with grep -l, for example grep -l '18Jun22h' *. You can use --null to pass a null character between the files, and send that to xargs:



                  $ grep -l '18Jun22h' * --null | xargs -0 rm --


                  I would recommend doing a sanity check first though, as with this 'create and then remove the files' demonstration:



                  $ for file in 'test file' 'file?name' 'file*name'; do echo foo > "$file"; done; ls -1
                  file*name
                  file?name
                  test file
                  $ grep -l foo * --null | xargs -L1 -0 echo
                  file*name
                  file?name
                  test file
                  $ grep -l foo * --null | xargs -L1 -0 rm --





                  share|improve this answer















                  You can get the filenames for files which contain a given expression with grep -l, for example grep -l '18Jun22h' *. You can use --null to pass a null character between the files, and send that to xargs:



                  $ grep -l '18Jun22h' * --null | xargs -0 rm --


                  I would recommend doing a sanity check first though, as with this 'create and then remove the files' demonstration:



                  $ for file in 'test file' 'file?name' 'file*name'; do echo foo > "$file"; done; ls -1
                  file*name
                  file?name
                  test file
                  $ grep -l foo * --null | xargs -L1 -0 echo
                  file*name
                  file?name
                  test file
                  $ grep -l foo * --null | xargs -L1 -0 rm --






                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  edited Jun 25 at 18:15


























                  answered Jun 25 at 18:09









                  DopeGhoti

                  39.7k54779




                  39.7k54779




















                      up vote
                      0
                      down vote













                      Way 1: using ./



                      rm -f ./'file name' and rm -f ./'file*name'



                      Way 2: using inode:



                      Get the inode from ls



                      ls -i "file name"



                      then



                      find . -inum "numberoofinode" -exec rm ;






                      share|improve this answer





















                      • Except for the note from the OP: “Any method that deletes them one by one would be completely useless to me due to the quantity of files.”
                        – Jeff Schaller
                        Jun 25 at 19:49















                      up vote
                      0
                      down vote













                      Way 1: using ./



                      rm -f ./'file name' and rm -f ./'file*name'



                      Way 2: using inode:



                      Get the inode from ls



                      ls -i "file name"



                      then



                      find . -inum "numberoofinode" -exec rm ;






                      share|improve this answer





















                      • Except for the note from the OP: “Any method that deletes them one by one would be completely useless to me due to the quantity of files.”
                        – Jeff Schaller
                        Jun 25 at 19:49













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      Way 1: using ./



                      rm -f ./'file name' and rm -f ./'file*name'



                      Way 2: using inode:



                      Get the inode from ls



                      ls -i "file name"



                      then



                      find . -inum "numberoofinode" -exec rm ;






                      share|improve this answer













                      Way 1: using ./



                      rm -f ./'file name' and rm -f ./'file*name'



                      Way 2: using inode:



                      Get the inode from ls



                      ls -i "file name"



                      then



                      find . -inum "numberoofinode" -exec rm ;







                      share|improve this answer













                      share|improve this answer



                      share|improve this answer











                      answered Jun 25 at 18:59









                      SivaPrasath

                      3,88611737




                      3,88611737











                      • Except for the note from the OP: “Any method that deletes them one by one would be completely useless to me due to the quantity of files.”
                        – Jeff Schaller
                        Jun 25 at 19:49

















                      • Except for the note from the OP: “Any method that deletes them one by one would be completely useless to me due to the quantity of files.”
                        – Jeff Schaller
                        Jun 25 at 19:49
















                      Except for the note from the OP: “Any method that deletes them one by one would be completely useless to me due to the quantity of files.”
                      – Jeff Schaller
                      Jun 25 at 19:49





                      Except for the note from the OP: “Any method that deletes them one by one would be completely useless to me due to the quantity of files.”
                      – Jeff Schaller
                      Jun 25 at 19:49













                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451810%2fdeleting-files-using-pattern-matching-for-files-containing-spaces-and-asterisks%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?