Bash script tar specific files with retention period

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












0















I am new to bash script. I need to keep file for retention period of one month and archive other.



Example:



File format: WE20160225.log (20160225 YYYYMMDD)



Say in the directory /oracle/Sales has 2 files:



WE20160130.log



WE20151201.log



I need to tar the WE20151201.log



But not WE20160130.log because the file is less than one month from now. How can I do this? (probably need to substring the filename)



I'm able to change directory but don't know what to do next. My logic is loop through the file and get the date from filename, then compare with current date.



I need to use the filename date, not the file modification date.



SALES_DIR="/oracle/sales/"
cd $SALES_DIR









share|improve this question



















  • 2





    Any reason not to use find to check on modification time (instead some number sequence that might, or might not be available as part of the filename and could be interpreted as a date? And then you create the tar file from the resulting file list.

    – Anthon
    Feb 25 '16 at 13:01











  • tar is not a tool that will expire files. backup software are.

    – Archemar
    Feb 25 '16 at 13:39











  • Can you expand on your question and confirm if the file modification dates line up with the filename date/times (if they do, find can be used, if not, some other technique is required).

    – EightBitTony
    Feb 25 '16 at 13:59















0















I am new to bash script. I need to keep file for retention period of one month and archive other.



Example:



File format: WE20160225.log (20160225 YYYYMMDD)



Say in the directory /oracle/Sales has 2 files:



WE20160130.log



WE20151201.log



I need to tar the WE20151201.log



But not WE20160130.log because the file is less than one month from now. How can I do this? (probably need to substring the filename)



I'm able to change directory but don't know what to do next. My logic is loop through the file and get the date from filename, then compare with current date.



I need to use the filename date, not the file modification date.



SALES_DIR="/oracle/sales/"
cd $SALES_DIR









share|improve this question



















  • 2





    Any reason not to use find to check on modification time (instead some number sequence that might, or might not be available as part of the filename and could be interpreted as a date? And then you create the tar file from the resulting file list.

    – Anthon
    Feb 25 '16 at 13:01











  • tar is not a tool that will expire files. backup software are.

    – Archemar
    Feb 25 '16 at 13:39











  • Can you expand on your question and confirm if the file modification dates line up with the filename date/times (if they do, find can be used, if not, some other technique is required).

    – EightBitTony
    Feb 25 '16 at 13:59













0












0








0








I am new to bash script. I need to keep file for retention period of one month and archive other.



Example:



File format: WE20160225.log (20160225 YYYYMMDD)



Say in the directory /oracle/Sales has 2 files:



WE20160130.log



WE20151201.log



I need to tar the WE20151201.log



But not WE20160130.log because the file is less than one month from now. How can I do this? (probably need to substring the filename)



I'm able to change directory but don't know what to do next. My logic is loop through the file and get the date from filename, then compare with current date.



I need to use the filename date, not the file modification date.



SALES_DIR="/oracle/sales/"
cd $SALES_DIR









share|improve this question
















I am new to bash script. I need to keep file for retention period of one month and archive other.



Example:



File format: WE20160225.log (20160225 YYYYMMDD)



Say in the directory /oracle/Sales has 2 files:



WE20160130.log



WE20151201.log



I need to tar the WE20151201.log



But not WE20160130.log because the file is less than one month from now. How can I do this? (probably need to substring the filename)



I'm able to change directory but don't know what to do next. My logic is loop through the file and get the date from filename, then compare with current date.



I need to use the filename date, not the file modification date.



SALES_DIR="/oracle/sales/"
cd $SALES_DIR






bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 25 '16 at 15:58







hades

















asked Feb 25 '16 at 12:45









hadeshades

1691312




1691312







  • 2





    Any reason not to use find to check on modification time (instead some number sequence that might, or might not be available as part of the filename and could be interpreted as a date? And then you create the tar file from the resulting file list.

    – Anthon
    Feb 25 '16 at 13:01











  • tar is not a tool that will expire files. backup software are.

    – Archemar
    Feb 25 '16 at 13:39











  • Can you expand on your question and confirm if the file modification dates line up with the filename date/times (if they do, find can be used, if not, some other technique is required).

    – EightBitTony
    Feb 25 '16 at 13:59












  • 2





    Any reason not to use find to check on modification time (instead some number sequence that might, or might not be available as part of the filename and could be interpreted as a date? And then you create the tar file from the resulting file list.

    – Anthon
    Feb 25 '16 at 13:01











  • tar is not a tool that will expire files. backup software are.

    – Archemar
    Feb 25 '16 at 13:39











  • Can you expand on your question and confirm if the file modification dates line up with the filename date/times (if they do, find can be used, if not, some other technique is required).

    – EightBitTony
    Feb 25 '16 at 13:59







2




2





Any reason not to use find to check on modification time (instead some number sequence that might, or might not be available as part of the filename and could be interpreted as a date? And then you create the tar file from the resulting file list.

– Anthon
Feb 25 '16 at 13:01





Any reason not to use find to check on modification time (instead some number sequence that might, or might not be available as part of the filename and could be interpreted as a date? And then you create the tar file from the resulting file list.

– Anthon
Feb 25 '16 at 13:01













tar is not a tool that will expire files. backup software are.

– Archemar
Feb 25 '16 at 13:39





tar is not a tool that will expire files. backup software are.

– Archemar
Feb 25 '16 at 13:39













Can you expand on your question and confirm if the file modification dates line up with the filename date/times (if they do, find can be used, if not, some other technique is required).

– EightBitTony
Feb 25 '16 at 13:59





Can you expand on your question and confirm if the file modification dates line up with the filename date/times (if they do, find can be used, if not, some other technique is required).

– EightBitTony
Feb 25 '16 at 13:59










2 Answers
2






active

oldest

votes


















0














You can use something like this:



find somedir/* -mtime +5 -exec rm ;


It used find to find files older than 5 days and passes the found file over to delete.






share|improve this answer






























    0














    Here is the script for compressing month old log files.



    #!/bin/bash
    files=($(find /oracle/Sales -mtime +30))
    tar cvfz backup.tar.gz "$files[@]"


    -mtime +number-of-days to describe the file which you need to backup.



    For deleting the older file try this



    find /oracle/Sales -mtime +30 | xargs -n1 echo rm



    If you get the right output then remove echo from the line.






    share|improve this answer

























    • Assumes (maybe correctly, but maybe not) that the file has a timestamp which matches the filename, maybe sales data is written one month in arrears or maybe the file timestamps are different for some other reason. This might work but it's not a certainty.

      – EightBitTony
      Feb 25 '16 at 13:58










    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%2f265721%2fbash-script-tar-specific-files-with-retention-period%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









    0














    You can use something like this:



    find somedir/* -mtime +5 -exec rm ;


    It used find to find files older than 5 days and passes the found file over to delete.






    share|improve this answer



























      0














      You can use something like this:



      find somedir/* -mtime +5 -exec rm ;


      It used find to find files older than 5 days and passes the found file over to delete.






      share|improve this answer

























        0












        0








        0







        You can use something like this:



        find somedir/* -mtime +5 -exec rm ;


        It used find to find files older than 5 days and passes the found file over to delete.






        share|improve this answer













        You can use something like this:



        find somedir/* -mtime +5 -exec rm ;


        It used find to find files older than 5 days and passes the found file over to delete.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 25 '16 at 13:06









        SaschaSascha

        1385




        1385























            0














            Here is the script for compressing month old log files.



            #!/bin/bash
            files=($(find /oracle/Sales -mtime +30))
            tar cvfz backup.tar.gz "$files[@]"


            -mtime +number-of-days to describe the file which you need to backup.



            For deleting the older file try this



            find /oracle/Sales -mtime +30 | xargs -n1 echo rm



            If you get the right output then remove echo from the line.






            share|improve this answer

























            • Assumes (maybe correctly, but maybe not) that the file has a timestamp which matches the filename, maybe sales data is written one month in arrears or maybe the file timestamps are different for some other reason. This might work but it's not a certainty.

              – EightBitTony
              Feb 25 '16 at 13:58















            0














            Here is the script for compressing month old log files.



            #!/bin/bash
            files=($(find /oracle/Sales -mtime +30))
            tar cvfz backup.tar.gz "$files[@]"


            -mtime +number-of-days to describe the file which you need to backup.



            For deleting the older file try this



            find /oracle/Sales -mtime +30 | xargs -n1 echo rm



            If you get the right output then remove echo from the line.






            share|improve this answer

























            • Assumes (maybe correctly, but maybe not) that the file has a timestamp which matches the filename, maybe sales data is written one month in arrears or maybe the file timestamps are different for some other reason. This might work but it's not a certainty.

              – EightBitTony
              Feb 25 '16 at 13:58













            0












            0








            0







            Here is the script for compressing month old log files.



            #!/bin/bash
            files=($(find /oracle/Sales -mtime +30))
            tar cvfz backup.tar.gz "$files[@]"


            -mtime +number-of-days to describe the file which you need to backup.



            For deleting the older file try this



            find /oracle/Sales -mtime +30 | xargs -n1 echo rm



            If you get the right output then remove echo from the line.






            share|improve this answer















            Here is the script for compressing month old log files.



            #!/bin/bash
            files=($(find /oracle/Sales -mtime +30))
            tar cvfz backup.tar.gz "$files[@]"


            -mtime +number-of-days to describe the file which you need to backup.



            For deleting the older file try this



            find /oracle/Sales -mtime +30 | xargs -n1 echo rm



            If you get the right output then remove echo from the line.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 25 '16 at 13:20

























            answered Feb 25 '16 at 13:09









            MongrelMongrel

            2,08131747




            2,08131747












            • Assumes (maybe correctly, but maybe not) that the file has a timestamp which matches the filename, maybe sales data is written one month in arrears or maybe the file timestamps are different for some other reason. This might work but it's not a certainty.

              – EightBitTony
              Feb 25 '16 at 13:58

















            • Assumes (maybe correctly, but maybe not) that the file has a timestamp which matches the filename, maybe sales data is written one month in arrears or maybe the file timestamps are different for some other reason. This might work but it's not a certainty.

              – EightBitTony
              Feb 25 '16 at 13:58
















            Assumes (maybe correctly, but maybe not) that the file has a timestamp which matches the filename, maybe sales data is written one month in arrears or maybe the file timestamps are different for some other reason. This might work but it's not a certainty.

            – EightBitTony
            Feb 25 '16 at 13:58





            Assumes (maybe correctly, but maybe not) that the file has a timestamp which matches the filename, maybe sales data is written one month in arrears or maybe the file timestamps are different for some other reason. This might work but it's not a certainty.

            – EightBitTony
            Feb 25 '16 at 13:58

















            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%2f265721%2fbash-script-tar-specific-files-with-retention-period%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

            Palaiologos

            The Forum (Inglewood, California)