How to quote arguments with xargsWhy does my shell script choke on whitespace or other special characters?Why...

How do you harvest carrots in creative mode?

What is the difference between "Grippe" and "Männergrippe"?

Obtaining the intermediate solutions in AMPL

Add newline to prompt if it's too long

How long do you think advanced cybernetic implants would plausibly last?

Is MOSFET active device?

Papers on arXiv solving the same problem at the same time

Why is 1. d4 Nf6 2. c4 e6 3. Bg5 almost never played?

Numbers Decrease while Letters Increase

Do Bayesian credible intervals treat the estimated parameter as a random variable?

How to find out the average duration of the peer-review process for a given journal?

Tex Quotes(UVa 272)

How do I, an introvert, communicate to my friend and only colleague, an extrovert, that I want to spend my scheduled breaks without them?

What are some interesting features that are common cross-linguistically but don't exist in English?

The No-Free-Lunch Theorem and K-NN consistency

Why do banks “park” their money at the European Central Bank?

How much does Commander Data weigh?

'Us students' - Does this apposition need a comma?

Round towards zero

Do they have Supervillain(s)?

Is there any way to keep a player from killing an NPC?

How do proponents of Sola Scriptura address the ministry of those Apostles who authored no parts of Scripture?

How do I get toddlers to stop asking for food every hour?

How to gently end involvement with an online community?



How to quote arguments with xargs


Why does my shell script choke on whitespace or other special characters?Why does xargs strip quotes from input?How can I find files and then use xargs to move them?Use xargs to move files from within a directoryxargs line too longMakes it a difference whether I use a different -n flag value for xargs in combination with find command ?Deleting files with spaces in their namesCombine xargs -I with sedRunning more than one command with xargsFind fullpath and filename under a directory then pass to an executable file as argumentsHave `xargs` run the arguments it's received every <maximum amount of time> instead of every `-n` arguments?xargs vertical alignment of output






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







13















Suppose that I want to delete all files in a folder that are greater than 1 MB.



$ find . -size +1M | xargs -0 rm


This will not delete files that have space in their names. So I want it to quote all arguments it sends to rm. If find gives it Some report.docx it should pass "Some report.docx" to rm.



How can I do that?










share|improve this question






















  • 1





    You should read this mywiki.wooledge.org/UsingFind?highlight=%28xargs%29 before doing anything with xargs. Also as the wiki suggests, don't use xargs without passing the -print0 to find.

    – Valentin Bajrami
    Dec 10 '14 at 8:38






  • 1





    More generally, see Why does my shell script choke on whitespace or other special characters?

    – Gilles
    Dec 10 '14 at 20:52











  • Use xargs -d$'n' to limit the delimiter to only new lines (and not spaces; this wouldn't process quotes etc. specially -- I've checked on a GNU system) -- the answer given in stackoverflow.com/a/33528111/94687

    – imz -- Ivan Zakharyaschev
    May 11 '17 at 12:31


















13















Suppose that I want to delete all files in a folder that are greater than 1 MB.



$ find . -size +1M | xargs -0 rm


This will not delete files that have space in their names. So I want it to quote all arguments it sends to rm. If find gives it Some report.docx it should pass "Some report.docx" to rm.



How can I do that?










share|improve this question






















  • 1





    You should read this mywiki.wooledge.org/UsingFind?highlight=%28xargs%29 before doing anything with xargs. Also as the wiki suggests, don't use xargs without passing the -print0 to find.

    – Valentin Bajrami
    Dec 10 '14 at 8:38






  • 1





    More generally, see Why does my shell script choke on whitespace or other special characters?

    – Gilles
    Dec 10 '14 at 20:52











  • Use xargs -d$'n' to limit the delimiter to only new lines (and not spaces; this wouldn't process quotes etc. specially -- I've checked on a GNU system) -- the answer given in stackoverflow.com/a/33528111/94687

    – imz -- Ivan Zakharyaschev
    May 11 '17 at 12:31














13












13








13


4






Suppose that I want to delete all files in a folder that are greater than 1 MB.



$ find . -size +1M | xargs -0 rm


This will not delete files that have space in their names. So I want it to quote all arguments it sends to rm. If find gives it Some report.docx it should pass "Some report.docx" to rm.



How can I do that?










share|improve this question
















Suppose that I want to delete all files in a folder that are greater than 1 MB.



$ find . -size +1M | xargs -0 rm


This will not delete files that have space in their names. So I want it to quote all arguments it sends to rm. If find gives it Some report.docx it should pass "Some report.docx" to rm.



How can I do that?







shell find quoting rm xargs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 10 '14 at 20:51









Gilles

571k138 gold badges1180 silver badges1692 bronze badges




571k138 gold badges1180 silver badges1692 bronze badges










asked Dec 10 '14 at 8:22









Kshitiz SharmaKshitiz Sharma

3,38910 gold badges45 silver badges61 bronze badges




3,38910 gold badges45 silver badges61 bronze badges











  • 1





    You should read this mywiki.wooledge.org/UsingFind?highlight=%28xargs%29 before doing anything with xargs. Also as the wiki suggests, don't use xargs without passing the -print0 to find.

    – Valentin Bajrami
    Dec 10 '14 at 8:38






  • 1





    More generally, see Why does my shell script choke on whitespace or other special characters?

    – Gilles
    Dec 10 '14 at 20:52











  • Use xargs -d$'n' to limit the delimiter to only new lines (and not spaces; this wouldn't process quotes etc. specially -- I've checked on a GNU system) -- the answer given in stackoverflow.com/a/33528111/94687

    – imz -- Ivan Zakharyaschev
    May 11 '17 at 12:31














  • 1





    You should read this mywiki.wooledge.org/UsingFind?highlight=%28xargs%29 before doing anything with xargs. Also as the wiki suggests, don't use xargs without passing the -print0 to find.

    – Valentin Bajrami
    Dec 10 '14 at 8:38






  • 1





    More generally, see Why does my shell script choke on whitespace or other special characters?

    – Gilles
    Dec 10 '14 at 20:52











  • Use xargs -d$'n' to limit the delimiter to only new lines (and not spaces; this wouldn't process quotes etc. specially -- I've checked on a GNU system) -- the answer given in stackoverflow.com/a/33528111/94687

    – imz -- Ivan Zakharyaschev
    May 11 '17 at 12:31








1




1





You should read this mywiki.wooledge.org/UsingFind?highlight=%28xargs%29 before doing anything with xargs. Also as the wiki suggests, don't use xargs without passing the -print0 to find.

– Valentin Bajrami
Dec 10 '14 at 8:38





You should read this mywiki.wooledge.org/UsingFind?highlight=%28xargs%29 before doing anything with xargs. Also as the wiki suggests, don't use xargs without passing the -print0 to find.

– Valentin Bajrami
Dec 10 '14 at 8:38




1




1





More generally, see Why does my shell script choke on whitespace or other special characters?

– Gilles
Dec 10 '14 at 20:52





More generally, see Why does my shell script choke on whitespace or other special characters?

– Gilles
Dec 10 '14 at 20:52













Use xargs -d$'n' to limit the delimiter to only new lines (and not spaces; this wouldn't process quotes etc. specially -- I've checked on a GNU system) -- the answer given in stackoverflow.com/a/33528111/94687

– imz -- Ivan Zakharyaschev
May 11 '17 at 12:31





Use xargs -d$'n' to limit the delimiter to only new lines (and not spaces; this wouldn't process quotes etc. specially -- I've checked on a GNU system) -- the answer given in stackoverflow.com/a/33528111/94687

– imz -- Ivan Zakharyaschev
May 11 '17 at 12:31










3 Answers
3






active

oldest

votes


















11















Simple use:



find . -size +1M -delete


If you insist using xargs and rm with find, just add -print0 in your command:



find . -size +1M -print0 | xargs -r0 rm --


Other way:



find . -size +1M -execdir rm -- {} +


From man find:



-print0
True; print the full file name on the standard output, followed by a null
character (instead of the newline character that -print uses). This allows file names
that contain newlines or other types of white space to be correctly interpreted by
programs that process the find output. This option corresponds to the -0 option of
xargs.





share|improve this answer




























  • This solves the problem but actually quoting the arguments would be useful in other cases. So I'll hold on for other answers. +1 for a simple solution though.

    – Kshitiz Sharma
    Dec 10 '14 at 8:33








  • 2





    @KshitizSharma No, you do not want to pass "Some report.docx" to rm, unless the file name contains the quotes. What you want is to pass Some report.docx unmolested to rm. KasiyA's answer (now) shows the general way to do that with find. [KasiyA: sorry for the wrong ping earlier.]

    – Gilles
    Dec 10 '14 at 21:22













  • @Gilles Correct. In bash I generally quote the string to pass it unmolested. So by quoting what I meant was to send filename as a single argument to rm instead of being split up into $0 and $1 etc.

    – Kshitiz Sharma
    Dec 11 '14 at 6:36











  • On OSX I do xargs -0 instead of xargs -r0.

    – Rolf
    Sep 13 '18 at 15:56



















8















Option -0 of xargs means that output from pipe is interpreted as null terminated items. In such case you also need to create input for the pipe with find ... -print0.






share|improve this answer

































    0















    I had a similar requirement and ended up using the -I switch to have a placeholder and I was able to quote it.



    find . -size +1M | xargs -I {} rm "{}"





    share|improve this answer








    New contributor



    dee-see is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.
























      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%2f172481%2fhow-to-quote-arguments-with-xargs%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      11















      Simple use:



      find . -size +1M -delete


      If you insist using xargs and rm with find, just add -print0 in your command:



      find . -size +1M -print0 | xargs -r0 rm --


      Other way:



      find . -size +1M -execdir rm -- {} +


      From man find:



      -print0
      True; print the full file name on the standard output, followed by a null
      character (instead of the newline character that -print uses). This allows file names
      that contain newlines or other types of white space to be correctly interpreted by
      programs that process the find output. This option corresponds to the -0 option of
      xargs.





      share|improve this answer




























      • This solves the problem but actually quoting the arguments would be useful in other cases. So I'll hold on for other answers. +1 for a simple solution though.

        – Kshitiz Sharma
        Dec 10 '14 at 8:33








      • 2





        @KshitizSharma No, you do not want to pass "Some report.docx" to rm, unless the file name contains the quotes. What you want is to pass Some report.docx unmolested to rm. KasiyA's answer (now) shows the general way to do that with find. [KasiyA: sorry for the wrong ping earlier.]

        – Gilles
        Dec 10 '14 at 21:22













      • @Gilles Correct. In bash I generally quote the string to pass it unmolested. So by quoting what I meant was to send filename as a single argument to rm instead of being split up into $0 and $1 etc.

        – Kshitiz Sharma
        Dec 11 '14 at 6:36











      • On OSX I do xargs -0 instead of xargs -r0.

        – Rolf
        Sep 13 '18 at 15:56
















      11















      Simple use:



      find . -size +1M -delete


      If you insist using xargs and rm with find, just add -print0 in your command:



      find . -size +1M -print0 | xargs -r0 rm --


      Other way:



      find . -size +1M -execdir rm -- {} +


      From man find:



      -print0
      True; print the full file name on the standard output, followed by a null
      character (instead of the newline character that -print uses). This allows file names
      that contain newlines or other types of white space to be correctly interpreted by
      programs that process the find output. This option corresponds to the -0 option of
      xargs.





      share|improve this answer




























      • This solves the problem but actually quoting the arguments would be useful in other cases. So I'll hold on for other answers. +1 for a simple solution though.

        – Kshitiz Sharma
        Dec 10 '14 at 8:33








      • 2





        @KshitizSharma No, you do not want to pass "Some report.docx" to rm, unless the file name contains the quotes. What you want is to pass Some report.docx unmolested to rm. KasiyA's answer (now) shows the general way to do that with find. [KasiyA: sorry for the wrong ping earlier.]

        – Gilles
        Dec 10 '14 at 21:22













      • @Gilles Correct. In bash I generally quote the string to pass it unmolested. So by quoting what I meant was to send filename as a single argument to rm instead of being split up into $0 and $1 etc.

        – Kshitiz Sharma
        Dec 11 '14 at 6:36











      • On OSX I do xargs -0 instead of xargs -r0.

        – Rolf
        Sep 13 '18 at 15:56














      11














      11










      11









      Simple use:



      find . -size +1M -delete


      If you insist using xargs and rm with find, just add -print0 in your command:



      find . -size +1M -print0 | xargs -r0 rm --


      Other way:



      find . -size +1M -execdir rm -- {} +


      From man find:



      -print0
      True; print the full file name on the standard output, followed by a null
      character (instead of the newline character that -print uses). This allows file names
      that contain newlines or other types of white space to be correctly interpreted by
      programs that process the find output. This option corresponds to the -0 option of
      xargs.





      share|improve this answer















      Simple use:



      find . -size +1M -delete


      If you insist using xargs and rm with find, just add -print0 in your command:



      find . -size +1M -print0 | xargs -r0 rm --


      Other way:



      find . -size +1M -execdir rm -- {} +


      From man find:



      -print0
      True; print the full file name on the standard output, followed by a null
      character (instead of the newline character that -print uses). This allows file names
      that contain newlines or other types of white space to be correctly interpreted by
      programs that process the find output. This option corresponds to the -0 option of
      xargs.






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Dec 11 '14 at 5:22

























      answered Dec 10 '14 at 8:24









      αғsнιηαғsнιη

      18.6k11 gold badges34 silver badges71 bronze badges




      18.6k11 gold badges34 silver badges71 bronze badges
















      • This solves the problem but actually quoting the arguments would be useful in other cases. So I'll hold on for other answers. +1 for a simple solution though.

        – Kshitiz Sharma
        Dec 10 '14 at 8:33








      • 2





        @KshitizSharma No, you do not want to pass "Some report.docx" to rm, unless the file name contains the quotes. What you want is to pass Some report.docx unmolested to rm. KasiyA's answer (now) shows the general way to do that with find. [KasiyA: sorry for the wrong ping earlier.]

        – Gilles
        Dec 10 '14 at 21:22













      • @Gilles Correct. In bash I generally quote the string to pass it unmolested. So by quoting what I meant was to send filename as a single argument to rm instead of being split up into $0 and $1 etc.

        – Kshitiz Sharma
        Dec 11 '14 at 6:36











      • On OSX I do xargs -0 instead of xargs -r0.

        – Rolf
        Sep 13 '18 at 15:56



















      • This solves the problem but actually quoting the arguments would be useful in other cases. So I'll hold on for other answers. +1 for a simple solution though.

        – Kshitiz Sharma
        Dec 10 '14 at 8:33








      • 2





        @KshitizSharma No, you do not want to pass "Some report.docx" to rm, unless the file name contains the quotes. What you want is to pass Some report.docx unmolested to rm. KasiyA's answer (now) shows the general way to do that with find. [KasiyA: sorry for the wrong ping earlier.]

        – Gilles
        Dec 10 '14 at 21:22













      • @Gilles Correct. In bash I generally quote the string to pass it unmolested. So by quoting what I meant was to send filename as a single argument to rm instead of being split up into $0 and $1 etc.

        – Kshitiz Sharma
        Dec 11 '14 at 6:36











      • On OSX I do xargs -0 instead of xargs -r0.

        – Rolf
        Sep 13 '18 at 15:56

















      This solves the problem but actually quoting the arguments would be useful in other cases. So I'll hold on for other answers. +1 for a simple solution though.

      – Kshitiz Sharma
      Dec 10 '14 at 8:33







      This solves the problem but actually quoting the arguments would be useful in other cases. So I'll hold on for other answers. +1 for a simple solution though.

      – Kshitiz Sharma
      Dec 10 '14 at 8:33






      2




      2





      @KshitizSharma No, you do not want to pass "Some report.docx" to rm, unless the file name contains the quotes. What you want is to pass Some report.docx unmolested to rm. KasiyA's answer (now) shows the general way to do that with find. [KasiyA: sorry for the wrong ping earlier.]

      – Gilles
      Dec 10 '14 at 21:22







      @KshitizSharma No, you do not want to pass "Some report.docx" to rm, unless the file name contains the quotes. What you want is to pass Some report.docx unmolested to rm. KasiyA's answer (now) shows the general way to do that with find. [KasiyA: sorry for the wrong ping earlier.]

      – Gilles
      Dec 10 '14 at 21:22















      @Gilles Correct. In bash I generally quote the string to pass it unmolested. So by quoting what I meant was to send filename as a single argument to rm instead of being split up into $0 and $1 etc.

      – Kshitiz Sharma
      Dec 11 '14 at 6:36





      @Gilles Correct. In bash I generally quote the string to pass it unmolested. So by quoting what I meant was to send filename as a single argument to rm instead of being split up into $0 and $1 etc.

      – Kshitiz Sharma
      Dec 11 '14 at 6:36













      On OSX I do xargs -0 instead of xargs -r0.

      – Rolf
      Sep 13 '18 at 15:56





      On OSX I do xargs -0 instead of xargs -r0.

      – Rolf
      Sep 13 '18 at 15:56













      8















      Option -0 of xargs means that output from pipe is interpreted as null terminated items. In such case you also need to create input for the pipe with find ... -print0.






      share|improve this answer






























        8















        Option -0 of xargs means that output from pipe is interpreted as null terminated items. In such case you also need to create input for the pipe with find ... -print0.






        share|improve this answer




























          8














          8










          8









          Option -0 of xargs means that output from pipe is interpreted as null terminated items. In such case you also need to create input for the pipe with find ... -print0.






          share|improve this answer













          Option -0 of xargs means that output from pipe is interpreted as null terminated items. In such case you also need to create input for the pipe with find ... -print0.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 10 '14 at 8:39









          jimmijjimmij

          33.9k8 gold badges82 silver badges116 bronze badges




          33.9k8 gold badges82 silver badges116 bronze badges


























              0















              I had a similar requirement and ended up using the -I switch to have a placeholder and I was able to quote it.



              find . -size +1M | xargs -I {} rm "{}"





              share|improve this answer








              New contributor



              dee-see is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.


























                0















                I had a similar requirement and ended up using the -I switch to have a placeholder and I was able to quote it.



                find . -size +1M | xargs -I {} rm "{}"





                share|improve this answer








                New contributor



                dee-see is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.
























                  0














                  0










                  0









                  I had a similar requirement and ended up using the -I switch to have a placeholder and I was able to quote it.



                  find . -size +1M | xargs -I {} rm "{}"





                  share|improve this answer








                  New contributor



                  dee-see is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  I had a similar requirement and ended up using the -I switch to have a placeholder and I was able to quote it.



                  find . -size +1M | xargs -I {} rm "{}"






                  share|improve this answer








                  New contributor



                  dee-see is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.








                  share|improve this answer



                  share|improve this answer






                  New contributor



                  dee-see is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.








                  answered yesterday









                  dee-seedee-see

                  1012 bronze badges




                  1012 bronze badges




                  New contributor



                  dee-see is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.




                  New contributor




                  dee-see is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.



































                      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%2f172481%2fhow-to-quote-arguments-with-xargs%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

                      Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                      Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                      Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...