Deleting files older than x number of years or days









up vote
0
down vote

favorite












I huge uploads folder and tried to delete the old files eg. files older than 3 years or files added before a certain date but for some reason it ends up deleting both new and old files.



Here are the commands I tried so far



Command 1



 find /home/user/uploads -maxdepth 1 -type f ! -newermt "2018-10-14 00:00:00" -exec rm -v ;


This command removes everything ignoring the with ! -newermt or just - newermt



Command 2



find /home/user/uploads -maxdepth 1 -mtime +1095 -type f -print -exec rm -i ;


It returned no result



Command 3



find /home/user/uploads -type f ! -newermt "2018-10-14 00:00:00" | xargs rm 


This returned an error "xargs: unmatched single quote by default quotes are special to xargs unless you use the -o option "



I can't figure out what exactly is wrong with the commands.. I'm running on a CentOS system.










share|improve this question





















  • find /home/user/uploads -maxdepth 1 -type f ! -newermt "2018-10-14 00:00:00" is correct for matching only files older than 2018-10-14 00:00:00". You say "This command removes everything" -- were there files newer than that deleted?
    – David C. Rankin
    Nov 11 at 10:58










  • yes... it removed new files that were created after this date, instead of files created before this date
    – iOflower
    Nov 11 at 11:05










  • OK, hold on. Let me come up with a test for you.
    – David C. Rankin
    Nov 11 at 11:08










  • Make a datetst dir, e.g. md datetst && cd datetst create a file for every day of Oct. 2018, e.g for i in 1..31; do touch -d "2018-10-$i" file_$i; done, Now let's find . -maxdepth 1 -type f ! -newermt "2018-10-16" | sort and see only files corresponding to days 1-16 are selected as not newer than "2018-10-16" which is right. Does you test given different results?
    – David C. Rankin
    Nov 11 at 11:18










  • Yes, i got the results I wanted after testing your code.. it sorted out the files created before 2018-10-16. ! -newermt sorted files before and just -newermt sorted the files modified after.. I'm going to try this with my command and post the results..
    – iOflower
    Nov 11 at 11:35














up vote
0
down vote

favorite












I huge uploads folder and tried to delete the old files eg. files older than 3 years or files added before a certain date but for some reason it ends up deleting both new and old files.



Here are the commands I tried so far



Command 1



 find /home/user/uploads -maxdepth 1 -type f ! -newermt "2018-10-14 00:00:00" -exec rm -v ;


This command removes everything ignoring the with ! -newermt or just - newermt



Command 2



find /home/user/uploads -maxdepth 1 -mtime +1095 -type f -print -exec rm -i ;


It returned no result



Command 3



find /home/user/uploads -type f ! -newermt "2018-10-14 00:00:00" | xargs rm 


This returned an error "xargs: unmatched single quote by default quotes are special to xargs unless you use the -o option "



I can't figure out what exactly is wrong with the commands.. I'm running on a CentOS system.










share|improve this question





















  • find /home/user/uploads -maxdepth 1 -type f ! -newermt "2018-10-14 00:00:00" is correct for matching only files older than 2018-10-14 00:00:00". You say "This command removes everything" -- were there files newer than that deleted?
    – David C. Rankin
    Nov 11 at 10:58










  • yes... it removed new files that were created after this date, instead of files created before this date
    – iOflower
    Nov 11 at 11:05










  • OK, hold on. Let me come up with a test for you.
    – David C. Rankin
    Nov 11 at 11:08










  • Make a datetst dir, e.g. md datetst && cd datetst create a file for every day of Oct. 2018, e.g for i in 1..31; do touch -d "2018-10-$i" file_$i; done, Now let's find . -maxdepth 1 -type f ! -newermt "2018-10-16" | sort and see only files corresponding to days 1-16 are selected as not newer than "2018-10-16" which is right. Does you test given different results?
    – David C. Rankin
    Nov 11 at 11:18










  • Yes, i got the results I wanted after testing your code.. it sorted out the files created before 2018-10-16. ! -newermt sorted files before and just -newermt sorted the files modified after.. I'm going to try this with my command and post the results..
    – iOflower
    Nov 11 at 11:35












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I huge uploads folder and tried to delete the old files eg. files older than 3 years or files added before a certain date but for some reason it ends up deleting both new and old files.



Here are the commands I tried so far



Command 1



 find /home/user/uploads -maxdepth 1 -type f ! -newermt "2018-10-14 00:00:00" -exec rm -v ;


This command removes everything ignoring the with ! -newermt or just - newermt



Command 2



find /home/user/uploads -maxdepth 1 -mtime +1095 -type f -print -exec rm -i ;


It returned no result



Command 3



find /home/user/uploads -type f ! -newermt "2018-10-14 00:00:00" | xargs rm 


This returned an error "xargs: unmatched single quote by default quotes are special to xargs unless you use the -o option "



I can't figure out what exactly is wrong with the commands.. I'm running on a CentOS system.










share|improve this question













I huge uploads folder and tried to delete the old files eg. files older than 3 years or files added before a certain date but for some reason it ends up deleting both new and old files.



Here are the commands I tried so far



Command 1



 find /home/user/uploads -maxdepth 1 -type f ! -newermt "2018-10-14 00:00:00" -exec rm -v ;


This command removes everything ignoring the with ! -newermt or just - newermt



Command 2



find /home/user/uploads -maxdepth 1 -mtime +1095 -type f -print -exec rm -i ;


It returned no result



Command 3



find /home/user/uploads -type f ! -newermt "2018-10-14 00:00:00" | xargs rm 


This returned an error "xargs: unmatched single quote by default quotes are special to xargs unless you use the -o option "



I can't figure out what exactly is wrong with the commands.. I'm running on a CentOS system.







shell unix xargs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 10:44









iOflower

248




248











  • find /home/user/uploads -maxdepth 1 -type f ! -newermt "2018-10-14 00:00:00" is correct for matching only files older than 2018-10-14 00:00:00". You say "This command removes everything" -- were there files newer than that deleted?
    – David C. Rankin
    Nov 11 at 10:58










  • yes... it removed new files that were created after this date, instead of files created before this date
    – iOflower
    Nov 11 at 11:05










  • OK, hold on. Let me come up with a test for you.
    – David C. Rankin
    Nov 11 at 11:08










  • Make a datetst dir, e.g. md datetst && cd datetst create a file for every day of Oct. 2018, e.g for i in 1..31; do touch -d "2018-10-$i" file_$i; done, Now let's find . -maxdepth 1 -type f ! -newermt "2018-10-16" | sort and see only files corresponding to days 1-16 are selected as not newer than "2018-10-16" which is right. Does you test given different results?
    – David C. Rankin
    Nov 11 at 11:18










  • Yes, i got the results I wanted after testing your code.. it sorted out the files created before 2018-10-16. ! -newermt sorted files before and just -newermt sorted the files modified after.. I'm going to try this with my command and post the results..
    – iOflower
    Nov 11 at 11:35
















  • find /home/user/uploads -maxdepth 1 -type f ! -newermt "2018-10-14 00:00:00" is correct for matching only files older than 2018-10-14 00:00:00". You say "This command removes everything" -- were there files newer than that deleted?
    – David C. Rankin
    Nov 11 at 10:58










  • yes... it removed new files that were created after this date, instead of files created before this date
    – iOflower
    Nov 11 at 11:05










  • OK, hold on. Let me come up with a test for you.
    – David C. Rankin
    Nov 11 at 11:08










  • Make a datetst dir, e.g. md datetst && cd datetst create a file for every day of Oct. 2018, e.g for i in 1..31; do touch -d "2018-10-$i" file_$i; done, Now let's find . -maxdepth 1 -type f ! -newermt "2018-10-16" | sort and see only files corresponding to days 1-16 are selected as not newer than "2018-10-16" which is right. Does you test given different results?
    – David C. Rankin
    Nov 11 at 11:18










  • Yes, i got the results I wanted after testing your code.. it sorted out the files created before 2018-10-16. ! -newermt sorted files before and just -newermt sorted the files modified after.. I'm going to try this with my command and post the results..
    – iOflower
    Nov 11 at 11:35















find /home/user/uploads -maxdepth 1 -type f ! -newermt "2018-10-14 00:00:00" is correct for matching only files older than 2018-10-14 00:00:00". You say "This command removes everything" -- were there files newer than that deleted?
– David C. Rankin
Nov 11 at 10:58




find /home/user/uploads -maxdepth 1 -type f ! -newermt "2018-10-14 00:00:00" is correct for matching only files older than 2018-10-14 00:00:00". You say "This command removes everything" -- were there files newer than that deleted?
– David C. Rankin
Nov 11 at 10:58












yes... it removed new files that were created after this date, instead of files created before this date
– iOflower
Nov 11 at 11:05




yes... it removed new files that were created after this date, instead of files created before this date
– iOflower
Nov 11 at 11:05












OK, hold on. Let me come up with a test for you.
– David C. Rankin
Nov 11 at 11:08




OK, hold on. Let me come up with a test for you.
– David C. Rankin
Nov 11 at 11:08












Make a datetst dir, e.g. md datetst && cd datetst create a file for every day of Oct. 2018, e.g for i in 1..31; do touch -d "2018-10-$i" file_$i; done, Now let's find . -maxdepth 1 -type f ! -newermt "2018-10-16" | sort and see only files corresponding to days 1-16 are selected as not newer than "2018-10-16" which is right. Does you test given different results?
– David C. Rankin
Nov 11 at 11:18




Make a datetst dir, e.g. md datetst && cd datetst create a file for every day of Oct. 2018, e.g for i in 1..31; do touch -d "2018-10-$i" file_$i; done, Now let's find . -maxdepth 1 -type f ! -newermt "2018-10-16" | sort and see only files corresponding to days 1-16 are selected as not newer than "2018-10-16" which is right. Does you test given different results?
– David C. Rankin
Nov 11 at 11:18












Yes, i got the results I wanted after testing your code.. it sorted out the files created before 2018-10-16. ! -newermt sorted files before and just -newermt sorted the files modified after.. I'm going to try this with my command and post the results..
– iOflower
Nov 11 at 11:35




Yes, i got the results I wanted after testing your code.. it sorted out the files created before 2018-10-16. ! -newermt sorted files before and just -newermt sorted the files modified after.. I'm going to try this with my command and post the results..
– iOflower
Nov 11 at 11:35












1 Answer
1






active

oldest

votes

















up vote
0
down vote













So after playing with the commands for a while this is the command that worked.



find . -maxdepth 1 -type f ! -newermt "2018-10-16" -exec rm -v ;


These flags also helped incase you're come across this situation.



-maxdepth 1 


means only files within that folder



-newermt 


used for files modified on that date or after..



! -newermt 


used for removing files before the said date.



I also added the -v flag so I would see the output of files been deleted.



Thanks for all the help this saved me a lot of time..






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fstackoverflow.com%2fquestions%2f53247956%2fdeleting-files-older-than-x-number-of-years-or-days%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    So after playing with the commands for a while this is the command that worked.



    find . -maxdepth 1 -type f ! -newermt "2018-10-16" -exec rm -v ;


    These flags also helped incase you're come across this situation.



    -maxdepth 1 


    means only files within that folder



    -newermt 


    used for files modified on that date or after..



    ! -newermt 


    used for removing files before the said date.



    I also added the -v flag so I would see the output of files been deleted.



    Thanks for all the help this saved me a lot of time..






    share|improve this answer
























      up vote
      0
      down vote













      So after playing with the commands for a while this is the command that worked.



      find . -maxdepth 1 -type f ! -newermt "2018-10-16" -exec rm -v ;


      These flags also helped incase you're come across this situation.



      -maxdepth 1 


      means only files within that folder



      -newermt 


      used for files modified on that date or after..



      ! -newermt 


      used for removing files before the said date.



      I also added the -v flag so I would see the output of files been deleted.



      Thanks for all the help this saved me a lot of time..






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        So after playing with the commands for a while this is the command that worked.



        find . -maxdepth 1 -type f ! -newermt "2018-10-16" -exec rm -v ;


        These flags also helped incase you're come across this situation.



        -maxdepth 1 


        means only files within that folder



        -newermt 


        used for files modified on that date or after..



        ! -newermt 


        used for removing files before the said date.



        I also added the -v flag so I would see the output of files been deleted.



        Thanks for all the help this saved me a lot of time..






        share|improve this answer












        So after playing with the commands for a while this is the command that worked.



        find . -maxdepth 1 -type f ! -newermt "2018-10-16" -exec rm -v ;


        These flags also helped incase you're come across this situation.



        -maxdepth 1 


        means only files within that folder



        -newermt 


        used for files modified on that date or after..



        ! -newermt 


        used for removing files before the said date.



        I also added the -v flag so I would see the output of files been deleted.



        Thanks for all the help this saved me a lot of time..







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 7:43









        iOflower

        248




        248



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • 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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2fstackoverflow.com%2fquestions%2f53247956%2fdeleting-files-older-than-x-number-of-years-or-days%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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto