How to maintain sort order with xargs and gunzip

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











up vote
3
down vote

favorite












I'm attempting to extract the contents of some files by alphabetical (which in this case also means date and iteration) order and when I test the process first with ls:



$ find /opt/minecraft/wonders/logs/ -name 20* -type f -mtime -3 -print0 
| sort | xargs -r0 ls -l | awk -F' ' 'print $6 " " $7 " " $9'


I get a positive result:



Aug 18 /opt/minecraft/wonders/logs/2018-08-17-3.log.gz
Aug 18 /opt/minecraft/wonders/logs/2018-08-18-1.log.gz
Aug 19 /opt/minecraft/wonders/logs/2018-08-18-2.log.gz
Aug 19 /opt/minecraft/wonders/logs/2018-08-19-1.log.gz
Aug 20 /opt/minecraft/wonders/logs/2018-08-19-2.log.gz
Aug 20 /opt/minecraft/wonders/logs/2018-08-20-1.log.gz


However, when I go to actually extract the files the sort order is lost:



$ find /opt/minecraft/wonders/logs/ -name 20* -type f -mtime -3 -print0 
| sort | xargs -r0 gunzip -vc | grep "/opt.*"`

/opt/minecraft/wonders/logs/2018-08-18-1.log.gz: 66.8%
/opt/minecraft/wonders/logs/2018-08-18-2.log.gz: 83.1%
/opt/minecraft/wonders/logs/2018-08-19-1.log.gz: 70.3%
/opt/minecraft/wonders/logs/2018-08-19-2.log.gz: 72.9%
/opt/minecraft/wonders/logs/2018-08-20-1.log.gz: 73.3%
/opt/minecraft/wonders/logs/2018-08-17-3.log.gz: 90.2%


How can I maintain the sort order while unzipping these files?







share|improve this question


























    up vote
    3
    down vote

    favorite












    I'm attempting to extract the contents of some files by alphabetical (which in this case also means date and iteration) order and when I test the process first with ls:



    $ find /opt/minecraft/wonders/logs/ -name 20* -type f -mtime -3 -print0 
    | sort | xargs -r0 ls -l | awk -F' ' 'print $6 " " $7 " " $9'


    I get a positive result:



    Aug 18 /opt/minecraft/wonders/logs/2018-08-17-3.log.gz
    Aug 18 /opt/minecraft/wonders/logs/2018-08-18-1.log.gz
    Aug 19 /opt/minecraft/wonders/logs/2018-08-18-2.log.gz
    Aug 19 /opt/minecraft/wonders/logs/2018-08-19-1.log.gz
    Aug 20 /opt/minecraft/wonders/logs/2018-08-19-2.log.gz
    Aug 20 /opt/minecraft/wonders/logs/2018-08-20-1.log.gz


    However, when I go to actually extract the files the sort order is lost:



    $ find /opt/minecraft/wonders/logs/ -name 20* -type f -mtime -3 -print0 
    | sort | xargs -r0 gunzip -vc | grep "/opt.*"`

    /opt/minecraft/wonders/logs/2018-08-18-1.log.gz: 66.8%
    /opt/minecraft/wonders/logs/2018-08-18-2.log.gz: 83.1%
    /opt/minecraft/wonders/logs/2018-08-19-1.log.gz: 70.3%
    /opt/minecraft/wonders/logs/2018-08-19-2.log.gz: 72.9%
    /opt/minecraft/wonders/logs/2018-08-20-1.log.gz: 73.3%
    /opt/minecraft/wonders/logs/2018-08-17-3.log.gz: 90.2%


    How can I maintain the sort order while unzipping these files?







    share|improve this question
























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I'm attempting to extract the contents of some files by alphabetical (which in this case also means date and iteration) order and when I test the process first with ls:



      $ find /opt/minecraft/wonders/logs/ -name 20* -type f -mtime -3 -print0 
      | sort | xargs -r0 ls -l | awk -F' ' 'print $6 " " $7 " " $9'


      I get a positive result:



      Aug 18 /opt/minecraft/wonders/logs/2018-08-17-3.log.gz
      Aug 18 /opt/minecraft/wonders/logs/2018-08-18-1.log.gz
      Aug 19 /opt/minecraft/wonders/logs/2018-08-18-2.log.gz
      Aug 19 /opt/minecraft/wonders/logs/2018-08-19-1.log.gz
      Aug 20 /opt/minecraft/wonders/logs/2018-08-19-2.log.gz
      Aug 20 /opt/minecraft/wonders/logs/2018-08-20-1.log.gz


      However, when I go to actually extract the files the sort order is lost:



      $ find /opt/minecraft/wonders/logs/ -name 20* -type f -mtime -3 -print0 
      | sort | xargs -r0 gunzip -vc | grep "/opt.*"`

      /opt/minecraft/wonders/logs/2018-08-18-1.log.gz: 66.8%
      /opt/minecraft/wonders/logs/2018-08-18-2.log.gz: 83.1%
      /opt/minecraft/wonders/logs/2018-08-19-1.log.gz: 70.3%
      /opt/minecraft/wonders/logs/2018-08-19-2.log.gz: 72.9%
      /opt/minecraft/wonders/logs/2018-08-20-1.log.gz: 73.3%
      /opt/minecraft/wonders/logs/2018-08-17-3.log.gz: 90.2%


      How can I maintain the sort order while unzipping these files?







      share|improve this question














      I'm attempting to extract the contents of some files by alphabetical (which in this case also means date and iteration) order and when I test the process first with ls:



      $ find /opt/minecraft/wonders/logs/ -name 20* -type f -mtime -3 -print0 
      | sort | xargs -r0 ls -l | awk -F' ' 'print $6 " " $7 " " $9'


      I get a positive result:



      Aug 18 /opt/minecraft/wonders/logs/2018-08-17-3.log.gz
      Aug 18 /opt/minecraft/wonders/logs/2018-08-18-1.log.gz
      Aug 19 /opt/minecraft/wonders/logs/2018-08-18-2.log.gz
      Aug 19 /opt/minecraft/wonders/logs/2018-08-19-1.log.gz
      Aug 20 /opt/minecraft/wonders/logs/2018-08-19-2.log.gz
      Aug 20 /opt/minecraft/wonders/logs/2018-08-20-1.log.gz


      However, when I go to actually extract the files the sort order is lost:



      $ find /opt/minecraft/wonders/logs/ -name 20* -type f -mtime -3 -print0 
      | sort | xargs -r0 gunzip -vc | grep "/opt.*"`

      /opt/minecraft/wonders/logs/2018-08-18-1.log.gz: 66.8%
      /opt/minecraft/wonders/logs/2018-08-18-2.log.gz: 83.1%
      /opt/minecraft/wonders/logs/2018-08-19-1.log.gz: 70.3%
      /opt/minecraft/wonders/logs/2018-08-19-2.log.gz: 72.9%
      /opt/minecraft/wonders/logs/2018-08-20-1.log.gz: 73.3%
      /opt/minecraft/wonders/logs/2018-08-17-3.log.gz: 90.2%


      How can I maintain the sort order while unzipping these files?









      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 21 at 3:53









      slm♦

      237k65485659




      237k65485659










      asked Aug 21 at 3:48









      Jesse Yishai

      184




      184




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          7
          down vote



          accepted










          You have used the -print0 option with find, and -0 with xargs, but you forgot to use -z for sort, so sort essentially sees a single line (unless your filenames contain n). The output you see with ls is probably ls doing some sorting.



          find /opt/minecraft/wonders/logs/ -name '20*' -type f -mtime -3 -print0 |
          sort -z | xargs -r0 gunzip -vc | grep /opt


          (Note: 20* is a glob and needs to be quoted for the shell so it's passed literally to find, you don't want to escape / for grep, what that does is unspecified, no need for .* at the end of the regexp if all you want is print the matching line)






          share|improve this answer






















          • This worked, thank you!
            – Jesse Yishai
            Aug 21 at 4:07






          • 2




            @JesseYishai If the answer helped you, don't forget to mark the answer as accepted by clicking on the check mark beside the answer ! :)
            – Ikaros
            Aug 21 at 8:29










          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%2f463769%2fhow-to-maintain-sort-order-with-xargs-and-gunzip%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          7
          down vote



          accepted










          You have used the -print0 option with find, and -0 with xargs, but you forgot to use -z for sort, so sort essentially sees a single line (unless your filenames contain n). The output you see with ls is probably ls doing some sorting.



          find /opt/minecraft/wonders/logs/ -name '20*' -type f -mtime -3 -print0 |
          sort -z | xargs -r0 gunzip -vc | grep /opt


          (Note: 20* is a glob and needs to be quoted for the shell so it's passed literally to find, you don't want to escape / for grep, what that does is unspecified, no need for .* at the end of the regexp if all you want is print the matching line)






          share|improve this answer






















          • This worked, thank you!
            – Jesse Yishai
            Aug 21 at 4:07






          • 2




            @JesseYishai If the answer helped you, don't forget to mark the answer as accepted by clicking on the check mark beside the answer ! :)
            – Ikaros
            Aug 21 at 8:29














          up vote
          7
          down vote



          accepted










          You have used the -print0 option with find, and -0 with xargs, but you forgot to use -z for sort, so sort essentially sees a single line (unless your filenames contain n). The output you see with ls is probably ls doing some sorting.



          find /opt/minecraft/wonders/logs/ -name '20*' -type f -mtime -3 -print0 |
          sort -z | xargs -r0 gunzip -vc | grep /opt


          (Note: 20* is a glob and needs to be quoted for the shell so it's passed literally to find, you don't want to escape / for grep, what that does is unspecified, no need for .* at the end of the regexp if all you want is print the matching line)






          share|improve this answer






















          • This worked, thank you!
            – Jesse Yishai
            Aug 21 at 4:07






          • 2




            @JesseYishai If the answer helped you, don't forget to mark the answer as accepted by clicking on the check mark beside the answer ! :)
            – Ikaros
            Aug 21 at 8:29












          up vote
          7
          down vote



          accepted







          up vote
          7
          down vote



          accepted






          You have used the -print0 option with find, and -0 with xargs, but you forgot to use -z for sort, so sort essentially sees a single line (unless your filenames contain n). The output you see with ls is probably ls doing some sorting.



          find /opt/minecraft/wonders/logs/ -name '20*' -type f -mtime -3 -print0 |
          sort -z | xargs -r0 gunzip -vc | grep /opt


          (Note: 20* is a glob and needs to be quoted for the shell so it's passed literally to find, you don't want to escape / for grep, what that does is unspecified, no need for .* at the end of the regexp if all you want is print the matching line)






          share|improve this answer














          You have used the -print0 option with find, and -0 with xargs, but you forgot to use -z for sort, so sort essentially sees a single line (unless your filenames contain n). The output you see with ls is probably ls doing some sorting.



          find /opt/minecraft/wonders/logs/ -name '20*' -type f -mtime -3 -print0 |
          sort -z | xargs -r0 gunzip -vc | grep /opt


          (Note: 20* is a glob and needs to be quoted for the shell so it's passed literally to find, you don't want to escape / for grep, what that does is unspecified, no need for .* at the end of the regexp if all you want is print the matching line)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 21 at 6:35









          Stéphane Chazelas

          282k53521854




          282k53521854










          answered Aug 21 at 3:59









          muru

          33.5k577144




          33.5k577144











          • This worked, thank you!
            – Jesse Yishai
            Aug 21 at 4:07






          • 2




            @JesseYishai If the answer helped you, don't forget to mark the answer as accepted by clicking on the check mark beside the answer ! :)
            – Ikaros
            Aug 21 at 8:29
















          • This worked, thank you!
            – Jesse Yishai
            Aug 21 at 4:07






          • 2




            @JesseYishai If the answer helped you, don't forget to mark the answer as accepted by clicking on the check mark beside the answer ! :)
            – Ikaros
            Aug 21 at 8:29















          This worked, thank you!
          – Jesse Yishai
          Aug 21 at 4:07




          This worked, thank you!
          – Jesse Yishai
          Aug 21 at 4:07




          2




          2




          @JesseYishai If the answer helped you, don't forget to mark the answer as accepted by clicking on the check mark beside the answer ! :)
          – Ikaros
          Aug 21 at 8:29




          @JesseYishai If the answer helped you, don't forget to mark the answer as accepted by clicking on the check mark beside the answer ! :)
          – Ikaros
          Aug 21 at 8:29

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f463769%2fhow-to-maintain-sort-order-with-xargs-and-gunzip%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          What does second last employer means? [closed]

          List of Gilmore Girls characters

          Confectionery