Deleting files with spaces in their names












9















I am trying to delete all the files with a space in their names. I am using following command. But it is giving me an error



Command : ls | egrep '. ' | xargs rm



Here if I am using only ls | egrep '. ' command it is giving me all the file name with spaces in the filenames. But when I am trying to pass the output to rm, all the spaces (leading or trailing) gets deleted. So my command is not getting properly executed.



Any pointers on how to delete the file having atleast one space in their name?










share|improve this question





























    9















    I am trying to delete all the files with a space in their names. I am using following command. But it is giving me an error



    Command : ls | egrep '. ' | xargs rm



    Here if I am using only ls | egrep '. ' command it is giving me all the file name with spaces in the filenames. But when I am trying to pass the output to rm, all the spaces (leading or trailing) gets deleted. So my command is not getting properly executed.



    Any pointers on how to delete the file having atleast one space in their name?










    share|improve this question



























      9












      9








      9


      2






      I am trying to delete all the files with a space in their names. I am using following command. But it is giving me an error



      Command : ls | egrep '. ' | xargs rm



      Here if I am using only ls | egrep '. ' command it is giving me all the file name with spaces in the filenames. But when I am trying to pass the output to rm, all the spaces (leading or trailing) gets deleted. So my command is not getting properly executed.



      Any pointers on how to delete the file having atleast one space in their name?










      share|improve this question
















      I am trying to delete all the files with a space in their names. I am using following command. But it is giving me an error



      Command : ls | egrep '. ' | xargs rm



      Here if I am using only ls | egrep '. ' command it is giving me all the file name with spaces in the filenames. But when I am trying to pass the output to rm, all the spaces (leading or trailing) gets deleted. So my command is not getting properly executed.



      Any pointers on how to delete the file having atleast one space in their name?







      shell quoting rm xargs






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 7 '15 at 23:52









      Gilles

      543k12811001617




      543k12811001617










      asked Jun 7 '15 at 21:38









      AnujAnuj

      48116




      48116






















          6 Answers
          6






          active

          oldest

          votes


















          29














          You can use standard globbing on the rm command:



          rm -- * *


          This will delete any file whose name contains a space; the space is escaped so the shell doesn't interpret it as a separator. Adding -- will avoid problems with filenames starting with dashes (they won’t be interpreted as arguments by rm).



          If you want to confirm each file before it’s deleted, add the -i option:



          rm -i -- * *





          share|improve this answer





















          • 4





            You will DEFINITELY want to run this through an echo first, to guard from typos. Add echo at the front and it will print out all the files it's going to remove.

            – Riking
            Jun 8 '15 at 3:00








          • 1





            Anuj, the reason why this has the most upvotes is that because though find is powerful, sometimes you don't need to kill the chicken with a machine gun. UNIX administrators would generally not resort to find to (for example) "remove all files beginning with the letter A"... one would simply rm A*. Likewise to remove files containing spaces, rm can do the job. In other words, don't be fooled because space is invisible and is treated specially by the shell. Simply escape it, as Stephen Kitt has done, and you can think of it like any other character.

            – Mike S
            Jun 8 '15 at 19:17





















          13














          I would avoid parsing ls output



          Why not :



          find . -type f -name '* *' -delete


          No problem with rm :-).



          Although this is recursive and will delete all files with space in current directory and nested directories, as mentionned in comments.






          share|improve this answer





















          • 4





            (1) you can use -name '* *' instead of the regex; and (2) you can use -print0 | xargs -0 rm -i to address @StephenKitt's concern.

            – Kevin
            Jun 8 '15 at 5:05






          • 1





            No need for xargs. Just use -exec rm -i '{}' +

            – R..
            Jun 8 '15 at 15:42






          • 1





            @Mhmd It's got more pitfalls than an 8-bit gaming console. mywiki.wooledge.org/ParsingLs

            – Daenyth
            Jun 8 '15 at 18:22








          • 1





            If you want to avoid subdirectories, use find . -maxdepth 1 -name '* *' -delete.

            – Mikkel
            Jun 8 '15 at 21:32






          • 1





            @BenjaminW. fixed ?

            – solsTiCe
            Nov 13 '18 at 11:56



















          7














          Look at this
          Suppose name "strange file"



          Solution one



          rm strange file


          solution two



          rm "strange file"


          solution three



          ls -i "strange file"


          you see the inode
          then



          find . -inum "numberoofinode" -exec rm {} ;


          In case of very strange file names like



          !-filename or --filename


          use



          rm ./'!-filename'





          share|improve this answer































            4














            From man xargs




            xargs reads items from the standard input, delimited by blanks (which
            can be protected with double or single quotes or a backslash) or
            newlines, and executes the command (default is /bin/echo) one or more
            times with any initial-arguments followed by items read from standard
            input. Blank lines on the standard input are ignored.




            We can (mostly) fix your initial command by changing the xargs delimiter to a newline:



            ls | egrep '. ' | xargs -d 'n' rm (don't do this... read on)



            But what if the filename contains a newline?



            touch "filename with blanks
            and newline"




            Because Unix filenames can contain blanks and newlines, this default
            behaviour is often problematic; filenames containing blanks and/or
            newlines are incorrectly processed by xargs. In these situations it is
            better to use the -0 option, which prevents such problems.




            ls is really a tool for direct consumption by a human, instead we need to use the find command which can separate the filenames with a null character (-print0). We also need to tell grep to use null characters to separate the input (-z) and output (-Z). Finally, we tell xargs to also use null characters (-0)



            find . -type f -print0 | egrep '. ' -z -Z | xargs -0 rm






            share|improve this answer

































              1














              You can use:



              find . -name '* *' -delete





              share|improve this answer


























              • rm -- * * seems better

                – Max ZHUANG
                Jun 8 '15 at 6:00











              • Also, this was covered by Kevin's comment on solsTiCe's answer.

                – G-Man
                Jun 8 '15 at 6:44



















              -1














              To do it that way, you will need the -Z option of grep, and the -0 option of xargs. But I would not do it that way (ls is not the right tool for the job, there are many problems in getting the computer to read its output).



              See other answers for a better way.





              Also



              ls | … is equivalent to ls -d * and echo * | …



              All of which have problems. Therefore don't use ls like this, use a solution from another answer.






              share|improve this answer


























              • Also ls * when pipes outputs the file names new-line delimited. echo (at least some echo implementations) expands backslash sequences. grep -Z is for writing file names NUL-delimited when using -l, it won't help here. If you meant -z, that won't help either as most ls implementations lack an option to output file names NUL delimited. One could do something like printf '%s' * | grep -z ' ' | xargs -r0 rm -f though.

                – Stéphane Chazelas
                Nov 12 '18 at 17:37






              • 1





                @Kusalananda I think I have fixed it.

                – ctrl-alt-delor
                Nov 13 '18 at 9:27











              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',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              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%2funix.stackexchange.com%2fquestions%2f208140%2fdeleting-files-with-spaces-in-their-names%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









              29














              You can use standard globbing on the rm command:



              rm -- * *


              This will delete any file whose name contains a space; the space is escaped so the shell doesn't interpret it as a separator. Adding -- will avoid problems with filenames starting with dashes (they won’t be interpreted as arguments by rm).



              If you want to confirm each file before it’s deleted, add the -i option:



              rm -i -- * *





              share|improve this answer





















              • 4





                You will DEFINITELY want to run this through an echo first, to guard from typos. Add echo at the front and it will print out all the files it's going to remove.

                – Riking
                Jun 8 '15 at 3:00








              • 1





                Anuj, the reason why this has the most upvotes is that because though find is powerful, sometimes you don't need to kill the chicken with a machine gun. UNIX administrators would generally not resort to find to (for example) "remove all files beginning with the letter A"... one would simply rm A*. Likewise to remove files containing spaces, rm can do the job. In other words, don't be fooled because space is invisible and is treated specially by the shell. Simply escape it, as Stephen Kitt has done, and you can think of it like any other character.

                – Mike S
                Jun 8 '15 at 19:17


















              29














              You can use standard globbing on the rm command:



              rm -- * *


              This will delete any file whose name contains a space; the space is escaped so the shell doesn't interpret it as a separator. Adding -- will avoid problems with filenames starting with dashes (they won’t be interpreted as arguments by rm).



              If you want to confirm each file before it’s deleted, add the -i option:



              rm -i -- * *





              share|improve this answer





















              • 4





                You will DEFINITELY want to run this through an echo first, to guard from typos. Add echo at the front and it will print out all the files it's going to remove.

                – Riking
                Jun 8 '15 at 3:00








              • 1





                Anuj, the reason why this has the most upvotes is that because though find is powerful, sometimes you don't need to kill the chicken with a machine gun. UNIX administrators would generally not resort to find to (for example) "remove all files beginning with the letter A"... one would simply rm A*. Likewise to remove files containing spaces, rm can do the job. In other words, don't be fooled because space is invisible and is treated specially by the shell. Simply escape it, as Stephen Kitt has done, and you can think of it like any other character.

                – Mike S
                Jun 8 '15 at 19:17
















              29












              29








              29







              You can use standard globbing on the rm command:



              rm -- * *


              This will delete any file whose name contains a space; the space is escaped so the shell doesn't interpret it as a separator. Adding -- will avoid problems with filenames starting with dashes (they won’t be interpreted as arguments by rm).



              If you want to confirm each file before it’s deleted, add the -i option:



              rm -i -- * *





              share|improve this answer















              You can use standard globbing on the rm command:



              rm -- * *


              This will delete any file whose name contains a space; the space is escaped so the shell doesn't interpret it as a separator. Adding -- will avoid problems with filenames starting with dashes (they won’t be interpreted as arguments by rm).



              If you want to confirm each file before it’s deleted, add the -i option:



              rm -i -- * *






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Feb 27 at 16:17

























              answered Jun 7 '15 at 21:41









              Stephen KittStephen Kitt

              177k24402479




              177k24402479








              • 4





                You will DEFINITELY want to run this through an echo first, to guard from typos. Add echo at the front and it will print out all the files it's going to remove.

                – Riking
                Jun 8 '15 at 3:00








              • 1





                Anuj, the reason why this has the most upvotes is that because though find is powerful, sometimes you don't need to kill the chicken with a machine gun. UNIX administrators would generally not resort to find to (for example) "remove all files beginning with the letter A"... one would simply rm A*. Likewise to remove files containing spaces, rm can do the job. In other words, don't be fooled because space is invisible and is treated specially by the shell. Simply escape it, as Stephen Kitt has done, and you can think of it like any other character.

                – Mike S
                Jun 8 '15 at 19:17
















              • 4





                You will DEFINITELY want to run this through an echo first, to guard from typos. Add echo at the front and it will print out all the files it's going to remove.

                – Riking
                Jun 8 '15 at 3:00








              • 1





                Anuj, the reason why this has the most upvotes is that because though find is powerful, sometimes you don't need to kill the chicken with a machine gun. UNIX administrators would generally not resort to find to (for example) "remove all files beginning with the letter A"... one would simply rm A*. Likewise to remove files containing spaces, rm can do the job. In other words, don't be fooled because space is invisible and is treated specially by the shell. Simply escape it, as Stephen Kitt has done, and you can think of it like any other character.

                – Mike S
                Jun 8 '15 at 19:17










              4




              4





              You will DEFINITELY want to run this through an echo first, to guard from typos. Add echo at the front and it will print out all the files it's going to remove.

              – Riking
              Jun 8 '15 at 3:00







              You will DEFINITELY want to run this through an echo first, to guard from typos. Add echo at the front and it will print out all the files it's going to remove.

              – Riking
              Jun 8 '15 at 3:00






              1




              1





              Anuj, the reason why this has the most upvotes is that because though find is powerful, sometimes you don't need to kill the chicken with a machine gun. UNIX administrators would generally not resort to find to (for example) "remove all files beginning with the letter A"... one would simply rm A*. Likewise to remove files containing spaces, rm can do the job. In other words, don't be fooled because space is invisible and is treated specially by the shell. Simply escape it, as Stephen Kitt has done, and you can think of it like any other character.

              – Mike S
              Jun 8 '15 at 19:17







              Anuj, the reason why this has the most upvotes is that because though find is powerful, sometimes you don't need to kill the chicken with a machine gun. UNIX administrators would generally not resort to find to (for example) "remove all files beginning with the letter A"... one would simply rm A*. Likewise to remove files containing spaces, rm can do the job. In other words, don't be fooled because space is invisible and is treated specially by the shell. Simply escape it, as Stephen Kitt has done, and you can think of it like any other character.

              – Mike S
              Jun 8 '15 at 19:17















              13














              I would avoid parsing ls output



              Why not :



              find . -type f -name '* *' -delete


              No problem with rm :-).



              Although this is recursive and will delete all files with space in current directory and nested directories, as mentionned in comments.






              share|improve this answer





















              • 4





                (1) you can use -name '* *' instead of the regex; and (2) you can use -print0 | xargs -0 rm -i to address @StephenKitt's concern.

                – Kevin
                Jun 8 '15 at 5:05






              • 1





                No need for xargs. Just use -exec rm -i '{}' +

                – R..
                Jun 8 '15 at 15:42






              • 1





                @Mhmd It's got more pitfalls than an 8-bit gaming console. mywiki.wooledge.org/ParsingLs

                – Daenyth
                Jun 8 '15 at 18:22








              • 1





                If you want to avoid subdirectories, use find . -maxdepth 1 -name '* *' -delete.

                – Mikkel
                Jun 8 '15 at 21:32






              • 1





                @BenjaminW. fixed ?

                – solsTiCe
                Nov 13 '18 at 11:56
















              13














              I would avoid parsing ls output



              Why not :



              find . -type f -name '* *' -delete


              No problem with rm :-).



              Although this is recursive and will delete all files with space in current directory and nested directories, as mentionned in comments.






              share|improve this answer





















              • 4





                (1) you can use -name '* *' instead of the regex; and (2) you can use -print0 | xargs -0 rm -i to address @StephenKitt's concern.

                – Kevin
                Jun 8 '15 at 5:05






              • 1





                No need for xargs. Just use -exec rm -i '{}' +

                – R..
                Jun 8 '15 at 15:42






              • 1





                @Mhmd It's got more pitfalls than an 8-bit gaming console. mywiki.wooledge.org/ParsingLs

                – Daenyth
                Jun 8 '15 at 18:22








              • 1





                If you want to avoid subdirectories, use find . -maxdepth 1 -name '* *' -delete.

                – Mikkel
                Jun 8 '15 at 21:32






              • 1





                @BenjaminW. fixed ?

                – solsTiCe
                Nov 13 '18 at 11:56














              13












              13








              13







              I would avoid parsing ls output



              Why not :



              find . -type f -name '* *' -delete


              No problem with rm :-).



              Although this is recursive and will delete all files with space in current directory and nested directories, as mentionned in comments.






              share|improve this answer















              I would avoid parsing ls output



              Why not :



              find . -type f -name '* *' -delete


              No problem with rm :-).



              Although this is recursive and will delete all files with space in current directory and nested directories, as mentionned in comments.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 13 '18 at 12:14

























              answered Jun 8 '15 at 0:45









              solsTiCesolsTiCe

              519310




              519310








              • 4





                (1) you can use -name '* *' instead of the regex; and (2) you can use -print0 | xargs -0 rm -i to address @StephenKitt's concern.

                – Kevin
                Jun 8 '15 at 5:05






              • 1





                No need for xargs. Just use -exec rm -i '{}' +

                – R..
                Jun 8 '15 at 15:42






              • 1





                @Mhmd It's got more pitfalls than an 8-bit gaming console. mywiki.wooledge.org/ParsingLs

                – Daenyth
                Jun 8 '15 at 18:22








              • 1





                If you want to avoid subdirectories, use find . -maxdepth 1 -name '* *' -delete.

                – Mikkel
                Jun 8 '15 at 21:32






              • 1





                @BenjaminW. fixed ?

                – solsTiCe
                Nov 13 '18 at 11:56














              • 4





                (1) you can use -name '* *' instead of the regex; and (2) you can use -print0 | xargs -0 rm -i to address @StephenKitt's concern.

                – Kevin
                Jun 8 '15 at 5:05






              • 1





                No need for xargs. Just use -exec rm -i '{}' +

                – R..
                Jun 8 '15 at 15:42






              • 1





                @Mhmd It's got more pitfalls than an 8-bit gaming console. mywiki.wooledge.org/ParsingLs

                – Daenyth
                Jun 8 '15 at 18:22








              • 1





                If you want to avoid subdirectories, use find . -maxdepth 1 -name '* *' -delete.

                – Mikkel
                Jun 8 '15 at 21:32






              • 1





                @BenjaminW. fixed ?

                – solsTiCe
                Nov 13 '18 at 11:56








              4




              4





              (1) you can use -name '* *' instead of the regex; and (2) you can use -print0 | xargs -0 rm -i to address @StephenKitt's concern.

              – Kevin
              Jun 8 '15 at 5:05





              (1) you can use -name '* *' instead of the regex; and (2) you can use -print0 | xargs -0 rm -i to address @StephenKitt's concern.

              – Kevin
              Jun 8 '15 at 5:05




              1




              1





              No need for xargs. Just use -exec rm -i '{}' +

              – R..
              Jun 8 '15 at 15:42





              No need for xargs. Just use -exec rm -i '{}' +

              – R..
              Jun 8 '15 at 15:42




              1




              1





              @Mhmd It's got more pitfalls than an 8-bit gaming console. mywiki.wooledge.org/ParsingLs

              – Daenyth
              Jun 8 '15 at 18:22







              @Mhmd It's got more pitfalls than an 8-bit gaming console. mywiki.wooledge.org/ParsingLs

              – Daenyth
              Jun 8 '15 at 18:22






              1




              1





              If you want to avoid subdirectories, use find . -maxdepth 1 -name '* *' -delete.

              – Mikkel
              Jun 8 '15 at 21:32





              If you want to avoid subdirectories, use find . -maxdepth 1 -name '* *' -delete.

              – Mikkel
              Jun 8 '15 at 21:32




              1




              1





              @BenjaminW. fixed ?

              – solsTiCe
              Nov 13 '18 at 11:56





              @BenjaminW. fixed ?

              – solsTiCe
              Nov 13 '18 at 11:56











              7














              Look at this
              Suppose name "strange file"



              Solution one



              rm strange file


              solution two



              rm "strange file"


              solution three



              ls -i "strange file"


              you see the inode
              then



              find . -inum "numberoofinode" -exec rm {} ;


              In case of very strange file names like



              !-filename or --filename


              use



              rm ./'!-filename'





              share|improve this answer




























                7














                Look at this
                Suppose name "strange file"



                Solution one



                rm strange file


                solution two



                rm "strange file"


                solution three



                ls -i "strange file"


                you see the inode
                then



                find . -inum "numberoofinode" -exec rm {} ;


                In case of very strange file names like



                !-filename or --filename


                use



                rm ./'!-filename'





                share|improve this answer


























                  7












                  7








                  7







                  Look at this
                  Suppose name "strange file"



                  Solution one



                  rm strange file


                  solution two



                  rm "strange file"


                  solution three



                  ls -i "strange file"


                  you see the inode
                  then



                  find . -inum "numberoofinode" -exec rm {} ;


                  In case of very strange file names like



                  !-filename or --filename


                  use



                  rm ./'!-filename'





                  share|improve this answer













                  Look at this
                  Suppose name "strange file"



                  Solution one



                  rm strange file


                  solution two



                  rm "strange file"


                  solution three



                  ls -i "strange file"


                  you see the inode
                  then



                  find . -inum "numberoofinode" -exec rm {} ;


                  In case of very strange file names like



                  !-filename or --filename


                  use



                  rm ./'!-filename'






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 7 '15 at 21:55









                  elbarnaelbarna

                  4,163123885




                  4,163123885























                      4














                      From man xargs




                      xargs reads items from the standard input, delimited by blanks (which
                      can be protected with double or single quotes or a backslash) or
                      newlines, and executes the command (default is /bin/echo) one or more
                      times with any initial-arguments followed by items read from standard
                      input. Blank lines on the standard input are ignored.




                      We can (mostly) fix your initial command by changing the xargs delimiter to a newline:



                      ls | egrep '. ' | xargs -d 'n' rm (don't do this... read on)



                      But what if the filename contains a newline?



                      touch "filename with blanks
                      and newline"




                      Because Unix filenames can contain blanks and newlines, this default
                      behaviour is often problematic; filenames containing blanks and/or
                      newlines are incorrectly processed by xargs. In these situations it is
                      better to use the -0 option, which prevents such problems.




                      ls is really a tool for direct consumption by a human, instead we need to use the find command which can separate the filenames with a null character (-print0). We also need to tell grep to use null characters to separate the input (-z) and output (-Z). Finally, we tell xargs to also use null characters (-0)



                      find . -type f -print0 | egrep '. ' -z -Z | xargs -0 rm






                      share|improve this answer






























                        4














                        From man xargs




                        xargs reads items from the standard input, delimited by blanks (which
                        can be protected with double or single quotes or a backslash) or
                        newlines, and executes the command (default is /bin/echo) one or more
                        times with any initial-arguments followed by items read from standard
                        input. Blank lines on the standard input are ignored.




                        We can (mostly) fix your initial command by changing the xargs delimiter to a newline:



                        ls | egrep '. ' | xargs -d 'n' rm (don't do this... read on)



                        But what if the filename contains a newline?



                        touch "filename with blanks
                        and newline"




                        Because Unix filenames can contain blanks and newlines, this default
                        behaviour is often problematic; filenames containing blanks and/or
                        newlines are incorrectly processed by xargs. In these situations it is
                        better to use the -0 option, which prevents such problems.




                        ls is really a tool for direct consumption by a human, instead we need to use the find command which can separate the filenames with a null character (-print0). We also need to tell grep to use null characters to separate the input (-z) and output (-Z). Finally, we tell xargs to also use null characters (-0)



                        find . -type f -print0 | egrep '. ' -z -Z | xargs -0 rm






                        share|improve this answer




























                          4












                          4








                          4







                          From man xargs




                          xargs reads items from the standard input, delimited by blanks (which
                          can be protected with double or single quotes or a backslash) or
                          newlines, and executes the command (default is /bin/echo) one or more
                          times with any initial-arguments followed by items read from standard
                          input. Blank lines on the standard input are ignored.




                          We can (mostly) fix your initial command by changing the xargs delimiter to a newline:



                          ls | egrep '. ' | xargs -d 'n' rm (don't do this... read on)



                          But what if the filename contains a newline?



                          touch "filename with blanks
                          and newline"




                          Because Unix filenames can contain blanks and newlines, this default
                          behaviour is often problematic; filenames containing blanks and/or
                          newlines are incorrectly processed by xargs. In these situations it is
                          better to use the -0 option, which prevents such problems.




                          ls is really a tool for direct consumption by a human, instead we need to use the find command which can separate the filenames with a null character (-print0). We also need to tell grep to use null characters to separate the input (-z) and output (-Z). Finally, we tell xargs to also use null characters (-0)



                          find . -type f -print0 | egrep '. ' -z -Z | xargs -0 rm






                          share|improve this answer















                          From man xargs




                          xargs reads items from the standard input, delimited by blanks (which
                          can be protected with double or single quotes or a backslash) or
                          newlines, and executes the command (default is /bin/echo) one or more
                          times with any initial-arguments followed by items read from standard
                          input. Blank lines on the standard input are ignored.




                          We can (mostly) fix your initial command by changing the xargs delimiter to a newline:



                          ls | egrep '. ' | xargs -d 'n' rm (don't do this... read on)



                          But what if the filename contains a newline?



                          touch "filename with blanks
                          and newline"




                          Because Unix filenames can contain blanks and newlines, this default
                          behaviour is often problematic; filenames containing blanks and/or
                          newlines are incorrectly processed by xargs. In these situations it is
                          better to use the -0 option, which prevents such problems.




                          ls is really a tool for direct consumption by a human, instead we need to use the find command which can separate the filenames with a null character (-print0). We also need to tell grep to use null characters to separate the input (-z) and output (-Z). Finally, we tell xargs to also use null characters (-0)



                          find . -type f -print0 | egrep '. ' -z -Z | xargs -0 rm







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Apr 13 '17 at 12:36









                          Community

                          1




                          1










                          answered Jun 8 '15 at 7:53









                          anjsimmoanjsimmo

                          412




                          412























                              1














                              You can use:



                              find . -name '* *' -delete





                              share|improve this answer


























                              • rm -- * * seems better

                                – Max ZHUANG
                                Jun 8 '15 at 6:00











                              • Also, this was covered by Kevin's comment on solsTiCe's answer.

                                – G-Man
                                Jun 8 '15 at 6:44
















                              1














                              You can use:



                              find . -name '* *' -delete





                              share|improve this answer


























                              • rm -- * * seems better

                                – Max ZHUANG
                                Jun 8 '15 at 6:00











                              • Also, this was covered by Kevin's comment on solsTiCe's answer.

                                – G-Man
                                Jun 8 '15 at 6:44














                              1












                              1








                              1







                              You can use:



                              find . -name '* *' -delete





                              share|improve this answer















                              You can use:



                              find . -name '* *' -delete






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Jun 8 '15 at 17:54









                              Mhmd

                              1317




                              1317










                              answered Jun 8 '15 at 5:58









                              Max ZHUANGMax ZHUANG

                              111




                              111













                              • rm -- * * seems better

                                – Max ZHUANG
                                Jun 8 '15 at 6:00











                              • Also, this was covered by Kevin's comment on solsTiCe's answer.

                                – G-Man
                                Jun 8 '15 at 6:44



















                              • rm -- * * seems better

                                – Max ZHUANG
                                Jun 8 '15 at 6:00











                              • Also, this was covered by Kevin's comment on solsTiCe's answer.

                                – G-Man
                                Jun 8 '15 at 6:44

















                              rm -- * * seems better

                              – Max ZHUANG
                              Jun 8 '15 at 6:00





                              rm -- * * seems better

                              – Max ZHUANG
                              Jun 8 '15 at 6:00













                              Also, this was covered by Kevin's comment on solsTiCe's answer.

                              – G-Man
                              Jun 8 '15 at 6:44





                              Also, this was covered by Kevin's comment on solsTiCe's answer.

                              – G-Man
                              Jun 8 '15 at 6:44











                              -1














                              To do it that way, you will need the -Z option of grep, and the -0 option of xargs. But I would not do it that way (ls is not the right tool for the job, there are many problems in getting the computer to read its output).



                              See other answers for a better way.





                              Also



                              ls | … is equivalent to ls -d * and echo * | …



                              All of which have problems. Therefore don't use ls like this, use a solution from another answer.






                              share|improve this answer


























                              • Also ls * when pipes outputs the file names new-line delimited. echo (at least some echo implementations) expands backslash sequences. grep -Z is for writing file names NUL-delimited when using -l, it won't help here. If you meant -z, that won't help either as most ls implementations lack an option to output file names NUL delimited. One could do something like printf '%s' * | grep -z ' ' | xargs -r0 rm -f though.

                                – Stéphane Chazelas
                                Nov 12 '18 at 17:37






                              • 1





                                @Kusalananda I think I have fixed it.

                                – ctrl-alt-delor
                                Nov 13 '18 at 9:27
















                              -1














                              To do it that way, you will need the -Z option of grep, and the -0 option of xargs. But I would not do it that way (ls is not the right tool for the job, there are many problems in getting the computer to read its output).



                              See other answers for a better way.





                              Also



                              ls | … is equivalent to ls -d * and echo * | …



                              All of which have problems. Therefore don't use ls like this, use a solution from another answer.






                              share|improve this answer


























                              • Also ls * when pipes outputs the file names new-line delimited. echo (at least some echo implementations) expands backslash sequences. grep -Z is for writing file names NUL-delimited when using -l, it won't help here. If you meant -z, that won't help either as most ls implementations lack an option to output file names NUL delimited. One could do something like printf '%s' * | grep -z ' ' | xargs -r0 rm -f though.

                                – Stéphane Chazelas
                                Nov 12 '18 at 17:37






                              • 1





                                @Kusalananda I think I have fixed it.

                                – ctrl-alt-delor
                                Nov 13 '18 at 9:27














                              -1












                              -1








                              -1







                              To do it that way, you will need the -Z option of grep, and the -0 option of xargs. But I would not do it that way (ls is not the right tool for the job, there are many problems in getting the computer to read its output).



                              See other answers for a better way.





                              Also



                              ls | … is equivalent to ls -d * and echo * | …



                              All of which have problems. Therefore don't use ls like this, use a solution from another answer.






                              share|improve this answer















                              To do it that way, you will need the -Z option of grep, and the -0 option of xargs. But I would not do it that way (ls is not the right tool for the job, there are many problems in getting the computer to read its output).



                              See other answers for a better way.





                              Also



                              ls | … is equivalent to ls -d * and echo * | …



                              All of which have problems. Therefore don't use ls like this, use a solution from another answer.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 13 '18 at 9:27

























                              answered Nov 12 '18 at 11:11









                              ctrl-alt-delorctrl-alt-delor

                              12k42561




                              12k42561













                              • Also ls * when pipes outputs the file names new-line delimited. echo (at least some echo implementations) expands backslash sequences. grep -Z is for writing file names NUL-delimited when using -l, it won't help here. If you meant -z, that won't help either as most ls implementations lack an option to output file names NUL delimited. One could do something like printf '%s' * | grep -z ' ' | xargs -r0 rm -f though.

                                – Stéphane Chazelas
                                Nov 12 '18 at 17:37






                              • 1





                                @Kusalananda I think I have fixed it.

                                – ctrl-alt-delor
                                Nov 13 '18 at 9:27



















                              • Also ls * when pipes outputs the file names new-line delimited. echo (at least some echo implementations) expands backslash sequences. grep -Z is for writing file names NUL-delimited when using -l, it won't help here. If you meant -z, that won't help either as most ls implementations lack an option to output file names NUL delimited. One could do something like printf '%s' * | grep -z ' ' | xargs -r0 rm -f though.

                                – Stéphane Chazelas
                                Nov 12 '18 at 17:37






                              • 1





                                @Kusalananda I think I have fixed it.

                                – ctrl-alt-delor
                                Nov 13 '18 at 9:27

















                              Also ls * when pipes outputs the file names new-line delimited. echo (at least some echo implementations) expands backslash sequences. grep -Z is for writing file names NUL-delimited when using -l, it won't help here. If you meant -z, that won't help either as most ls implementations lack an option to output file names NUL delimited. One could do something like printf '%s' * | grep -z ' ' | xargs -r0 rm -f though.

                              – Stéphane Chazelas
                              Nov 12 '18 at 17:37





                              Also ls * when pipes outputs the file names new-line delimited. echo (at least some echo implementations) expands backslash sequences. grep -Z is for writing file names NUL-delimited when using -l, it won't help here. If you meant -z, that won't help either as most ls implementations lack an option to output file names NUL delimited. One could do something like printf '%s' * | grep -z ' ' | xargs -r0 rm -f though.

                              – Stéphane Chazelas
                              Nov 12 '18 at 17:37




                              1




                              1





                              @Kusalananda I think I have fixed it.

                              – ctrl-alt-delor
                              Nov 13 '18 at 9:27





                              @Kusalananda I think I have fixed it.

                              – ctrl-alt-delor
                              Nov 13 '18 at 9:27


















                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f208140%2fdeleting-files-with-spaces-in-their-names%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              How to make a Squid Proxy server?

                              Is this a new Fibonacci Identity?

                              19世紀