Zipping the 10 most recent files

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





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








0















Basically my requirement is to find 10 latest logs and compress it into zip. I have tried command ls -Lt | head -10 | zip log.zip *.log, but output is not matched.










share|improve this question
























  • Did you consider using 'logrotate' with a dedicated schedule? You are able to specify how old the files may get before being compressed.

    – gerhard d.
    Mar 14 at 8:17

















0















Basically my requirement is to find 10 latest logs and compress it into zip. I have tried command ls -Lt | head -10 | zip log.zip *.log, but output is not matched.










share|improve this question
























  • Did you consider using 'logrotate' with a dedicated schedule? You are able to specify how old the files may get before being compressed.

    – gerhard d.
    Mar 14 at 8:17













0












0








0


0






Basically my requirement is to find 10 latest logs and compress it into zip. I have tried command ls -Lt | head -10 | zip log.zip *.log, but output is not matched.










share|improve this question
















Basically my requirement is to find 10 latest logs and compress it into zip. I have tried command ls -Lt | head -10 | zip log.zip *.log, but output is not matched.







linux files date zip






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 14 at 15:25









Jeff Schaller

45k1164147




45k1164147










asked Mar 14 at 8:02









Prabhat JaiswalPrabhat Jaiswal

31




31












  • Did you consider using 'logrotate' with a dedicated schedule? You are able to specify how old the files may get before being compressed.

    – gerhard d.
    Mar 14 at 8:17

















  • Did you consider using 'logrotate' with a dedicated schedule? You are able to specify how old the files may get before being compressed.

    – gerhard d.
    Mar 14 at 8:17
















Did you consider using 'logrotate' with a dedicated schedule? You are able to specify how old the files may get before being compressed.

– gerhard d.
Mar 14 at 8:17





Did you consider using 'logrotate' with a dedicated schedule? You are able to specify how old the files may get before being compressed.

– gerhard d.
Mar 14 at 8:17










3 Answers
3






active

oldest

votes


















1














First take backup



You should try:



ls -Lt | head -10 | xargs zip log.zip 


Your command



zip log.zip *.log


is trying to compress every file in the current directory which is ending with .log, it is not taking file names from STDIN.



If your directory has mixed files, i.e. files other than log files, then you can try:



ls -Lt *.log | head -10 | xargs zip log.zip





share|improve this answer























  • thanks , ls -Lt | head -10 | xargs zip log.zip here zip is created in same directory i want that zip is goes to different directory so how can specify with this command]

    – Prabhat Jaiswal
    Mar 14 at 9:51











  • Instead of log.zip use the absolute path like /home/username/...../log.zip.

    – Prvt_Yadv
    Mar 14 at 10:04







  • 1





    be warned that if any of the filenames (ever) contain spaces, tabs, or newlines, then they will be mangled and lost in the current pipeline.

    – Jeff Schaller
    Mar 14 at 19:13


















2














To safely select the 10 most recent (plain) files in the current directory, I would recommend zsh, since it can safely, natively, select files based on modification time:



zsh -c 'zip log.zip *.log(.om[1,10])'


This uses two of zsh's wildcard ("glob") qualifiers and a subscripting operator:




  • *.log( ... ) -- this starts the wildcard off with *.log, which will select every1 file in the current directory that ends with .log, filtered by the following criteria


  • . -- this filters the resulting list to include only plain files


  • om -- this sorts ("orders") the resulting list by modification time, most recent first


  • [1,10] -- this narrows the resulting list by selecting elements 1 through 10 (the ten most recent files)

Once zsh has generated the 10 most recent plain files, it hands those to the zip command.




Footnotes:



  1. by default, zsh will not select hidden (dot) files, such as .foo.log; if you have such files and wish to select them, you can include the D glob qualifier (*.log(.Dom[1,10])) or set the GLOB_DOTS option (with setopt globdots).





share|improve this answer






























    0














    Tried with below command



    ls -ltrh| awk '$1 ~ /-rw/print $0'|sed -n '1,10p'| awk 'print "zip" " " $NF".zip" " " $NF'| sh





    share|improve this answer























    • Please don't try to parse ls. Besides the usual trouble, what if the file(s) aren't writable by the user-- causing the /-rw/ to fail?

      – Jeff Schaller
      Mar 14 at 19:05











    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%2f506230%2fzipping-the-10-most-recent-files%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









    1














    First take backup



    You should try:



    ls -Lt | head -10 | xargs zip log.zip 


    Your command



    zip log.zip *.log


    is trying to compress every file in the current directory which is ending with .log, it is not taking file names from STDIN.



    If your directory has mixed files, i.e. files other than log files, then you can try:



    ls -Lt *.log | head -10 | xargs zip log.zip





    share|improve this answer























    • thanks , ls -Lt | head -10 | xargs zip log.zip here zip is created in same directory i want that zip is goes to different directory so how can specify with this command]

      – Prabhat Jaiswal
      Mar 14 at 9:51











    • Instead of log.zip use the absolute path like /home/username/...../log.zip.

      – Prvt_Yadv
      Mar 14 at 10:04







    • 1





      be warned that if any of the filenames (ever) contain spaces, tabs, or newlines, then they will be mangled and lost in the current pipeline.

      – Jeff Schaller
      Mar 14 at 19:13















    1














    First take backup



    You should try:



    ls -Lt | head -10 | xargs zip log.zip 


    Your command



    zip log.zip *.log


    is trying to compress every file in the current directory which is ending with .log, it is not taking file names from STDIN.



    If your directory has mixed files, i.e. files other than log files, then you can try:



    ls -Lt *.log | head -10 | xargs zip log.zip





    share|improve this answer























    • thanks , ls -Lt | head -10 | xargs zip log.zip here zip is created in same directory i want that zip is goes to different directory so how can specify with this command]

      – Prabhat Jaiswal
      Mar 14 at 9:51











    • Instead of log.zip use the absolute path like /home/username/...../log.zip.

      – Prvt_Yadv
      Mar 14 at 10:04







    • 1





      be warned that if any of the filenames (ever) contain spaces, tabs, or newlines, then they will be mangled and lost in the current pipeline.

      – Jeff Schaller
      Mar 14 at 19:13













    1












    1








    1







    First take backup



    You should try:



    ls -Lt | head -10 | xargs zip log.zip 


    Your command



    zip log.zip *.log


    is trying to compress every file in the current directory which is ending with .log, it is not taking file names from STDIN.



    If your directory has mixed files, i.e. files other than log files, then you can try:



    ls -Lt *.log | head -10 | xargs zip log.zip





    share|improve this answer













    First take backup



    You should try:



    ls -Lt | head -10 | xargs zip log.zip 


    Your command



    zip log.zip *.log


    is trying to compress every file in the current directory which is ending with .log, it is not taking file names from STDIN.



    If your directory has mixed files, i.e. files other than log files, then you can try:



    ls -Lt *.log | head -10 | xargs zip log.zip






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 14 at 8:28









    Prvt_YadvPrvt_Yadv

    3,30631430




    3,30631430












    • thanks , ls -Lt | head -10 | xargs zip log.zip here zip is created in same directory i want that zip is goes to different directory so how can specify with this command]

      – Prabhat Jaiswal
      Mar 14 at 9:51











    • Instead of log.zip use the absolute path like /home/username/...../log.zip.

      – Prvt_Yadv
      Mar 14 at 10:04







    • 1





      be warned that if any of the filenames (ever) contain spaces, tabs, or newlines, then they will be mangled and lost in the current pipeline.

      – Jeff Schaller
      Mar 14 at 19:13

















    • thanks , ls -Lt | head -10 | xargs zip log.zip here zip is created in same directory i want that zip is goes to different directory so how can specify with this command]

      – Prabhat Jaiswal
      Mar 14 at 9:51











    • Instead of log.zip use the absolute path like /home/username/...../log.zip.

      – Prvt_Yadv
      Mar 14 at 10:04







    • 1





      be warned that if any of the filenames (ever) contain spaces, tabs, or newlines, then they will be mangled and lost in the current pipeline.

      – Jeff Schaller
      Mar 14 at 19:13
















    thanks , ls -Lt | head -10 | xargs zip log.zip here zip is created in same directory i want that zip is goes to different directory so how can specify with this command]

    – Prabhat Jaiswal
    Mar 14 at 9:51





    thanks , ls -Lt | head -10 | xargs zip log.zip here zip is created in same directory i want that zip is goes to different directory so how can specify with this command]

    – Prabhat Jaiswal
    Mar 14 at 9:51













    Instead of log.zip use the absolute path like /home/username/...../log.zip.

    – Prvt_Yadv
    Mar 14 at 10:04






    Instead of log.zip use the absolute path like /home/username/...../log.zip.

    – Prvt_Yadv
    Mar 14 at 10:04





    1




    1





    be warned that if any of the filenames (ever) contain spaces, tabs, or newlines, then they will be mangled and lost in the current pipeline.

    – Jeff Schaller
    Mar 14 at 19:13





    be warned that if any of the filenames (ever) contain spaces, tabs, or newlines, then they will be mangled and lost in the current pipeline.

    – Jeff Schaller
    Mar 14 at 19:13













    2














    To safely select the 10 most recent (plain) files in the current directory, I would recommend zsh, since it can safely, natively, select files based on modification time:



    zsh -c 'zip log.zip *.log(.om[1,10])'


    This uses two of zsh's wildcard ("glob") qualifiers and a subscripting operator:




    • *.log( ... ) -- this starts the wildcard off with *.log, which will select every1 file in the current directory that ends with .log, filtered by the following criteria


    • . -- this filters the resulting list to include only plain files


    • om -- this sorts ("orders") the resulting list by modification time, most recent first


    • [1,10] -- this narrows the resulting list by selecting elements 1 through 10 (the ten most recent files)

    Once zsh has generated the 10 most recent plain files, it hands those to the zip command.




    Footnotes:



    1. by default, zsh will not select hidden (dot) files, such as .foo.log; if you have such files and wish to select them, you can include the D glob qualifier (*.log(.Dom[1,10])) or set the GLOB_DOTS option (with setopt globdots).





    share|improve this answer



























      2














      To safely select the 10 most recent (plain) files in the current directory, I would recommend zsh, since it can safely, natively, select files based on modification time:



      zsh -c 'zip log.zip *.log(.om[1,10])'


      This uses two of zsh's wildcard ("glob") qualifiers and a subscripting operator:




      • *.log( ... ) -- this starts the wildcard off with *.log, which will select every1 file in the current directory that ends with .log, filtered by the following criteria


      • . -- this filters the resulting list to include only plain files


      • om -- this sorts ("orders") the resulting list by modification time, most recent first


      • [1,10] -- this narrows the resulting list by selecting elements 1 through 10 (the ten most recent files)

      Once zsh has generated the 10 most recent plain files, it hands those to the zip command.




      Footnotes:



      1. by default, zsh will not select hidden (dot) files, such as .foo.log; if you have such files and wish to select them, you can include the D glob qualifier (*.log(.Dom[1,10])) or set the GLOB_DOTS option (with setopt globdots).





      share|improve this answer

























        2












        2








        2







        To safely select the 10 most recent (plain) files in the current directory, I would recommend zsh, since it can safely, natively, select files based on modification time:



        zsh -c 'zip log.zip *.log(.om[1,10])'


        This uses two of zsh's wildcard ("glob") qualifiers and a subscripting operator:




        • *.log( ... ) -- this starts the wildcard off with *.log, which will select every1 file in the current directory that ends with .log, filtered by the following criteria


        • . -- this filters the resulting list to include only plain files


        • om -- this sorts ("orders") the resulting list by modification time, most recent first


        • [1,10] -- this narrows the resulting list by selecting elements 1 through 10 (the ten most recent files)

        Once zsh has generated the 10 most recent plain files, it hands those to the zip command.




        Footnotes:



        1. by default, zsh will not select hidden (dot) files, such as .foo.log; if you have such files and wish to select them, you can include the D glob qualifier (*.log(.Dom[1,10])) or set the GLOB_DOTS option (with setopt globdots).





        share|improve this answer













        To safely select the 10 most recent (plain) files in the current directory, I would recommend zsh, since it can safely, natively, select files based on modification time:



        zsh -c 'zip log.zip *.log(.om[1,10])'


        This uses two of zsh's wildcard ("glob") qualifiers and a subscripting operator:




        • *.log( ... ) -- this starts the wildcard off with *.log, which will select every1 file in the current directory that ends with .log, filtered by the following criteria


        • . -- this filters the resulting list to include only plain files


        • om -- this sorts ("orders") the resulting list by modification time, most recent first


        • [1,10] -- this narrows the resulting list by selecting elements 1 through 10 (the ten most recent files)

        Once zsh has generated the 10 most recent plain files, it hands those to the zip command.




        Footnotes:



        1. by default, zsh will not select hidden (dot) files, such as .foo.log; if you have such files and wish to select them, you can include the D glob qualifier (*.log(.Dom[1,10])) or set the GLOB_DOTS option (with setopt globdots).






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 14 at 12:54









        Jeff SchallerJeff Schaller

        45k1164147




        45k1164147





















            0














            Tried with below command



            ls -ltrh| awk '$1 ~ /-rw/print $0'|sed -n '1,10p'| awk 'print "zip" " " $NF".zip" " " $NF'| sh





            share|improve this answer























            • Please don't try to parse ls. Besides the usual trouble, what if the file(s) aren't writable by the user-- causing the /-rw/ to fail?

              – Jeff Schaller
              Mar 14 at 19:05















            0














            Tried with below command



            ls -ltrh| awk '$1 ~ /-rw/print $0'|sed -n '1,10p'| awk 'print "zip" " " $NF".zip" " " $NF'| sh





            share|improve this answer























            • Please don't try to parse ls. Besides the usual trouble, what if the file(s) aren't writable by the user-- causing the /-rw/ to fail?

              – Jeff Schaller
              Mar 14 at 19:05













            0












            0








            0







            Tried with below command



            ls -ltrh| awk '$1 ~ /-rw/print $0'|sed -n '1,10p'| awk 'print "zip" " " $NF".zip" " " $NF'| sh





            share|improve this answer













            Tried with below command



            ls -ltrh| awk '$1 ~ /-rw/print $0'|sed -n '1,10p'| awk 'print "zip" " " $NF".zip" " " $NF'| sh






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 14 at 18:36









            Praveen Kumar BSPraveen Kumar BS

            1,7621311




            1,7621311












            • Please don't try to parse ls. Besides the usual trouble, what if the file(s) aren't writable by the user-- causing the /-rw/ to fail?

              – Jeff Schaller
              Mar 14 at 19:05

















            • Please don't try to parse ls. Besides the usual trouble, what if the file(s) aren't writable by the user-- causing the /-rw/ to fail?

              – Jeff Schaller
              Mar 14 at 19:05
















            Please don't try to parse ls. Besides the usual trouble, what if the file(s) aren't writable by the user-- causing the /-rw/ to fail?

            – Jeff Schaller
            Mar 14 at 19:05





            Please don't try to parse ls. Besides the usual trouble, what if the file(s) aren't writable by the user-- causing the /-rw/ to fail?

            – Jeff Schaller
            Mar 14 at 19:05

















            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%2f506230%2fzipping-the-10-most-recent-files%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

            Peggy Mitchell

            The Forum (Inglewood, California)

            Palaiologos