Remove old files in a directory except files present in an exception file

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











up vote
3
down vote

favorite












I am writing a shell script to delete old files (older than 60 days) in a directory except few files and these file names are maintained in an exception file present in another directory.



I know the following command works for one exception file but i need to check a list of exception files



find . ! -name 'file.txt' -type f -exec rm -f +









share|improve this question























  • related: Delete all files in a directory whose name do not match a line in a file list
    – don_crissti
    Jun 28 '16 at 17:17














up vote
3
down vote

favorite












I am writing a shell script to delete old files (older than 60 days) in a directory except few files and these file names are maintained in an exception file present in another directory.



I know the following command works for one exception file but i need to check a list of exception files



find . ! -name 'file.txt' -type f -exec rm -f +









share|improve this question























  • related: Delete all files in a directory whose name do not match a line in a file list
    – don_crissti
    Jun 28 '16 at 17:17












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I am writing a shell script to delete old files (older than 60 days) in a directory except few files and these file names are maintained in an exception file present in another directory.



I know the following command works for one exception file but i need to check a list of exception files



find . ! -name 'file.txt' -type f -exec rm -f +









share|improve this question















I am writing a shell script to delete old files (older than 60 days) in a directory except few files and these file names are maintained in an exception file present in another directory.



I know the following command works for one exception file but i need to check a list of exception files



find . ! -name 'file.txt' -type f -exec rm -f +






shell-script find ksh






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 22:52









Rui F Ribeiro

38.2k1475126




38.2k1475126










asked Jun 28 '16 at 16:32









Balaji

183




183











  • related: Delete all files in a directory whose name do not match a line in a file list
    – don_crissti
    Jun 28 '16 at 17:17
















  • related: Delete all files in a directory whose name do not match a line in a file list
    – don_crissti
    Jun 28 '16 at 17:17















related: Delete all files in a directory whose name do not match a line in a file list
– don_crissti
Jun 28 '16 at 17:17




related: Delete all files in a directory whose name do not match a line in a file list
– don_crissti
Jun 28 '16 at 17:17










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Assuming your files have sane names (i.e. they don't have embedded newlines), something like this should work:



find . -mtime +60 | fgrep -v -x -f exceptions.txt | xargs -d 'n' rm -f


Replace rm -f with ls -1 for a dry run first. Put paths exactly as they are printed by find in exceptions.txt.






share|improve this answer




















  • Thanks for your input. Is fgrep taking output from find command and eliminating the files present in exceptions.txt and sending the result to rm command ?
    – Balaji
    Jun 28 '16 at 17:17










  • find prints a list of files, one per line. fgrep filters out the files in exceptions.txt. xargs ... rm -f removes the files.
    – Satō Katsura
    Jun 28 '16 at 17:20










  • Works perfectly !! Thanks for your help !!
    – Balaji
    Jun 28 '16 at 17:58

















up vote
1
down vote













I don't think find has an option like this, you could build a command using printf and your exclude list:



find . -name "*.txt" $(printf "! -name %s " $(cat file.txt)) -mtime +60 -exec rm -f +


file.txt will have list of files to exclude in find command.






share|improve this answer






















    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: 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%2f292643%2fremove-old-files-in-a-directory-except-files-present-in-an-exception-file%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    Assuming your files have sane names (i.e. they don't have embedded newlines), something like this should work:



    find . -mtime +60 | fgrep -v -x -f exceptions.txt | xargs -d 'n' rm -f


    Replace rm -f with ls -1 for a dry run first. Put paths exactly as they are printed by find in exceptions.txt.






    share|improve this answer




















    • Thanks for your input. Is fgrep taking output from find command and eliminating the files present in exceptions.txt and sending the result to rm command ?
      – Balaji
      Jun 28 '16 at 17:17










    • find prints a list of files, one per line. fgrep filters out the files in exceptions.txt. xargs ... rm -f removes the files.
      – Satō Katsura
      Jun 28 '16 at 17:20










    • Works perfectly !! Thanks for your help !!
      – Balaji
      Jun 28 '16 at 17:58














    up vote
    2
    down vote



    accepted










    Assuming your files have sane names (i.e. they don't have embedded newlines), something like this should work:



    find . -mtime +60 | fgrep -v -x -f exceptions.txt | xargs -d 'n' rm -f


    Replace rm -f with ls -1 for a dry run first. Put paths exactly as they are printed by find in exceptions.txt.






    share|improve this answer




















    • Thanks for your input. Is fgrep taking output from find command and eliminating the files present in exceptions.txt and sending the result to rm command ?
      – Balaji
      Jun 28 '16 at 17:17










    • find prints a list of files, one per line. fgrep filters out the files in exceptions.txt. xargs ... rm -f removes the files.
      – Satō Katsura
      Jun 28 '16 at 17:20










    • Works perfectly !! Thanks for your help !!
      – Balaji
      Jun 28 '16 at 17:58












    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    Assuming your files have sane names (i.e. they don't have embedded newlines), something like this should work:



    find . -mtime +60 | fgrep -v -x -f exceptions.txt | xargs -d 'n' rm -f


    Replace rm -f with ls -1 for a dry run first. Put paths exactly as they are printed by find in exceptions.txt.






    share|improve this answer












    Assuming your files have sane names (i.e. they don't have embedded newlines), something like this should work:



    find . -mtime +60 | fgrep -v -x -f exceptions.txt | xargs -d 'n' rm -f


    Replace rm -f with ls -1 for a dry run first. Put paths exactly as they are printed by find in exceptions.txt.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jun 28 '16 at 16:39









    Satō Katsura

    10.9k11534




    10.9k11534











    • Thanks for your input. Is fgrep taking output from find command and eliminating the files present in exceptions.txt and sending the result to rm command ?
      – Balaji
      Jun 28 '16 at 17:17










    • find prints a list of files, one per line. fgrep filters out the files in exceptions.txt. xargs ... rm -f removes the files.
      – Satō Katsura
      Jun 28 '16 at 17:20










    • Works perfectly !! Thanks for your help !!
      – Balaji
      Jun 28 '16 at 17:58
















    • Thanks for your input. Is fgrep taking output from find command and eliminating the files present in exceptions.txt and sending the result to rm command ?
      – Balaji
      Jun 28 '16 at 17:17










    • find prints a list of files, one per line. fgrep filters out the files in exceptions.txt. xargs ... rm -f removes the files.
      – Satō Katsura
      Jun 28 '16 at 17:20










    • Works perfectly !! Thanks for your help !!
      – Balaji
      Jun 28 '16 at 17:58















    Thanks for your input. Is fgrep taking output from find command and eliminating the files present in exceptions.txt and sending the result to rm command ?
    – Balaji
    Jun 28 '16 at 17:17




    Thanks for your input. Is fgrep taking output from find command and eliminating the files present in exceptions.txt and sending the result to rm command ?
    – Balaji
    Jun 28 '16 at 17:17












    find prints a list of files, one per line. fgrep filters out the files in exceptions.txt. xargs ... rm -f removes the files.
    – Satō Katsura
    Jun 28 '16 at 17:20




    find prints a list of files, one per line. fgrep filters out the files in exceptions.txt. xargs ... rm -f removes the files.
    – Satō Katsura
    Jun 28 '16 at 17:20












    Works perfectly !! Thanks for your help !!
    – Balaji
    Jun 28 '16 at 17:58




    Works perfectly !! Thanks for your help !!
    – Balaji
    Jun 28 '16 at 17:58












    up vote
    1
    down vote













    I don't think find has an option like this, you could build a command using printf and your exclude list:



    find . -name "*.txt" $(printf "! -name %s " $(cat file.txt)) -mtime +60 -exec rm -f +


    file.txt will have list of files to exclude in find command.






    share|improve this answer


























      up vote
      1
      down vote













      I don't think find has an option like this, you could build a command using printf and your exclude list:



      find . -name "*.txt" $(printf "! -name %s " $(cat file.txt)) -mtime +60 -exec rm -f +


      file.txt will have list of files to exclude in find command.






      share|improve this answer
























        up vote
        1
        down vote










        up vote
        1
        down vote









        I don't think find has an option like this, you could build a command using printf and your exclude list:



        find . -name "*.txt" $(printf "! -name %s " $(cat file.txt)) -mtime +60 -exec rm -f +


        file.txt will have list of files to exclude in find command.






        share|improve this answer














        I don't think find has an option like this, you could build a command using printf and your exclude list:



        find . -name "*.txt" $(printf "! -name %s " $(cat file.txt)) -mtime +60 -exec rm -f +


        file.txt will have list of files to exclude in find command.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jun 28 '16 at 16:50

























        answered Jun 28 '16 at 16:41









        Rahul

        8,82412842




        8,82412842



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f292643%2fremove-old-files-in-a-directory-except-files-present-in-an-exception-file%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

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

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay