Delete files older than X days with lastest modified file

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











up vote
1
down vote

favorite
1












Is there is any solution to delete the files based on comparing their modified date with the most recently modified file? The older files need to be deleted with respect to most recent file.



For example, if the last modified date is 09-10-2017, it means the files that are 10 days older need to be deleted. Same if the modified date is changed; the files to be deleted should change accordingly.










share|improve this question



















  • 1




    what if Last modified date is 01-03-2017 ?
    – RomanPerekhrest
    Oct 9 '17 at 10:06











  • You want to delete the files that are more than 10 days older than the most recently modified file?
    – Stéphane Chazelas
    Oct 9 '17 at 10:09










  • yes, It needs to deleted accordingly with latest file
    – Vignesh Vignasivakara
    Oct 9 '17 at 10:58














up vote
1
down vote

favorite
1












Is there is any solution to delete the files based on comparing their modified date with the most recently modified file? The older files need to be deleted with respect to most recent file.



For example, if the last modified date is 09-10-2017, it means the files that are 10 days older need to be deleted. Same if the modified date is changed; the files to be deleted should change accordingly.










share|improve this question



















  • 1




    what if Last modified date is 01-03-2017 ?
    – RomanPerekhrest
    Oct 9 '17 at 10:06











  • You want to delete the files that are more than 10 days older than the most recently modified file?
    – Stéphane Chazelas
    Oct 9 '17 at 10:09










  • yes, It needs to deleted accordingly with latest file
    – Vignesh Vignasivakara
    Oct 9 '17 at 10:58












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





Is there is any solution to delete the files based on comparing their modified date with the most recently modified file? The older files need to be deleted with respect to most recent file.



For example, if the last modified date is 09-10-2017, it means the files that are 10 days older need to be deleted. Same if the modified date is changed; the files to be deleted should change accordingly.










share|improve this question















Is there is any solution to delete the files based on comparing their modified date with the most recently modified file? The older files need to be deleted with respect to most recent file.



For example, if the last modified date is 09-10-2017, it means the files that are 10 days older need to be deleted. Same if the modified date is changed; the files to be deleted should change accordingly.







linux files rm timestamps






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 9 '17 at 12:38









Jeff Schaller

32.3k849109




32.3k849109










asked Oct 9 '17 at 10:00









Vignesh Vignasivakara

61




61







  • 1




    what if Last modified date is 01-03-2017 ?
    – RomanPerekhrest
    Oct 9 '17 at 10:06











  • You want to delete the files that are more than 10 days older than the most recently modified file?
    – Stéphane Chazelas
    Oct 9 '17 at 10:09










  • yes, It needs to deleted accordingly with latest file
    – Vignesh Vignasivakara
    Oct 9 '17 at 10:58












  • 1




    what if Last modified date is 01-03-2017 ?
    – RomanPerekhrest
    Oct 9 '17 at 10:06











  • You want to delete the files that are more than 10 days older than the most recently modified file?
    – Stéphane Chazelas
    Oct 9 '17 at 10:09










  • yes, It needs to deleted accordingly with latest file
    – Vignesh Vignasivakara
    Oct 9 '17 at 10:58







1




1




what if Last modified date is 01-03-2017 ?
– RomanPerekhrest
Oct 9 '17 at 10:06





what if Last modified date is 01-03-2017 ?
– RomanPerekhrest
Oct 9 '17 at 10:06













You want to delete the files that are more than 10 days older than the most recently modified file?
– Stéphane Chazelas
Oct 9 '17 at 10:09




You want to delete the files that are more than 10 days older than the most recently modified file?
– Stéphane Chazelas
Oct 9 '17 at 10:09












yes, It needs to deleted accordingly with latest file
– Vignesh Vignasivakara
Oct 9 '17 at 10:58




yes, It needs to deleted accordingly with latest file
– Vignesh Vignasivakara
Oct 9 '17 at 10:58










2 Answers
2






active

oldest

votes

















up vote
3
down vote













With GNU tools:



find . -type f -printf '%T@:%p' |
sort -rzn |
awk -v RS='' -v ORS='' -F: -v d=10 '
NR == 1 t = $1 - 86400 * d
$1 < t, 0 sub(/[^:]*:/, ""); print' |
xargs -r0 echo rm


(remove the echo when happy).



We're getting find to print 123456.123:./path/to/file<NUL> for each file (with 123456.123 being the last modification time of the files).



We're sorting that numerically (so the newest file comes first) and then using awk to find the ones that are more than 10 days older than the newest file (found in the first record in that sorted list).






share|improve this answer





























    up vote
    0
    down vote













    I feel below command would help if you are working on Linux based Operating system.



    find /path/to/directory/ -mindepth 1 -mtime +7 -delete



    +7 = variable which means files older than 7 days will be deleted



    Be careful with spaces, quotes to rm.






    share|improve this answer




















    • But that doesn’t answer the question, which is about deleting the latest modified file and files older than 10 days.
      – Stephen Kitt
      Jul 24 at 13:14










    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%2f396972%2fdelete-files-older-than-x-days-with-lastest-modified-file%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    With GNU tools:



    find . -type f -printf '%T@:%p' |
    sort -rzn |
    awk -v RS='' -v ORS='' -F: -v d=10 '
    NR == 1 t = $1 - 86400 * d
    $1 < t, 0 sub(/[^:]*:/, ""); print' |
    xargs -r0 echo rm


    (remove the echo when happy).



    We're getting find to print 123456.123:./path/to/file<NUL> for each file (with 123456.123 being the last modification time of the files).



    We're sorting that numerically (so the newest file comes first) and then using awk to find the ones that are more than 10 days older than the newest file (found in the first record in that sorted list).






    share|improve this answer


























      up vote
      3
      down vote













      With GNU tools:



      find . -type f -printf '%T@:%p' |
      sort -rzn |
      awk -v RS='' -v ORS='' -F: -v d=10 '
      NR == 1 t = $1 - 86400 * d
      $1 < t, 0 sub(/[^:]*:/, ""); print' |
      xargs -r0 echo rm


      (remove the echo when happy).



      We're getting find to print 123456.123:./path/to/file<NUL> for each file (with 123456.123 being the last modification time of the files).



      We're sorting that numerically (so the newest file comes first) and then using awk to find the ones that are more than 10 days older than the newest file (found in the first record in that sorted list).






      share|improve this answer
























        up vote
        3
        down vote










        up vote
        3
        down vote









        With GNU tools:



        find . -type f -printf '%T@:%p' |
        sort -rzn |
        awk -v RS='' -v ORS='' -F: -v d=10 '
        NR == 1 t = $1 - 86400 * d
        $1 < t, 0 sub(/[^:]*:/, ""); print' |
        xargs -r0 echo rm


        (remove the echo when happy).



        We're getting find to print 123456.123:./path/to/file<NUL> for each file (with 123456.123 being the last modification time of the files).



        We're sorting that numerically (so the newest file comes first) and then using awk to find the ones that are more than 10 days older than the newest file (found in the first record in that sorted list).






        share|improve this answer














        With GNU tools:



        find . -type f -printf '%T@:%p' |
        sort -rzn |
        awk -v RS='' -v ORS='' -F: -v d=10 '
        NR == 1 t = $1 - 86400 * d
        $1 < t, 0 sub(/[^:]*:/, ""); print' |
        xargs -r0 echo rm


        (remove the echo when happy).



        We're getting find to print 123456.123:./path/to/file<NUL> for each file (with 123456.123 being the last modification time of the files).



        We're sorting that numerically (so the newest file comes first) and then using awk to find the ones that are more than 10 days older than the newest file (found in the first record in that sorted list).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Oct 12 '17 at 5:21

























        answered Oct 9 '17 at 10:17









        Stéphane Chazelas

        283k53522859




        283k53522859






















            up vote
            0
            down vote













            I feel below command would help if you are working on Linux based Operating system.



            find /path/to/directory/ -mindepth 1 -mtime +7 -delete



            +7 = variable which means files older than 7 days will be deleted



            Be careful with spaces, quotes to rm.






            share|improve this answer




















            • But that doesn’t answer the question, which is about deleting the latest modified file and files older than 10 days.
              – Stephen Kitt
              Jul 24 at 13:14














            up vote
            0
            down vote













            I feel below command would help if you are working on Linux based Operating system.



            find /path/to/directory/ -mindepth 1 -mtime +7 -delete



            +7 = variable which means files older than 7 days will be deleted



            Be careful with spaces, quotes to rm.






            share|improve this answer




















            • But that doesn’t answer the question, which is about deleting the latest modified file and files older than 10 days.
              – Stephen Kitt
              Jul 24 at 13:14












            up vote
            0
            down vote










            up vote
            0
            down vote









            I feel below command would help if you are working on Linux based Operating system.



            find /path/to/directory/ -mindepth 1 -mtime +7 -delete



            +7 = variable which means files older than 7 days will be deleted



            Be careful with spaces, quotes to rm.






            share|improve this answer












            I feel below command would help if you are working on Linux based Operating system.



            find /path/to/directory/ -mindepth 1 -mtime +7 -delete



            +7 = variable which means files older than 7 days will be deleted



            Be careful with spaces, quotes to rm.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 24 at 9:29









            GURUVEER

            11




            11











            • But that doesn’t answer the question, which is about deleting the latest modified file and files older than 10 days.
              – Stephen Kitt
              Jul 24 at 13:14
















            • But that doesn’t answer the question, which is about deleting the latest modified file and files older than 10 days.
              – Stephen Kitt
              Jul 24 at 13:14















            But that doesn’t answer the question, which is about deleting the latest modified file and files older than 10 days.
            – Stephen Kitt
            Jul 24 at 13:14




            But that doesn’t answer the question, which is about deleting the latest modified file and files older than 10 days.
            – Stephen Kitt
            Jul 24 at 13:14

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f396972%2fdelete-files-older-than-x-days-with-lastest-modified-file%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

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

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay