automate Zipping files on dailybasis

arun kumar 71 Reputation points
2021-10-21T07:21:23.043+00:00

Hi Team

daily lot of log files are generating in some particular folder in Linux VM. i want to zip those files on date wise for example 15-20 log files are generating today that should be zipped to one file. likewise the files generating tomorrow should be zipped to one file.

please help me with the suitable command so that i can make it automate by adding that in crontab so that files should get zipped on date wise.

Operations Manager
Operations Manager
A family of System Center products that provide infrastructure monitoring, help ensure the predictable performance and availability of vital applications, and offer comprehensive monitoring for datacenters and cloud, both private and public.
1,410 questions
{count} votes

4 answers

Sort by: Most helpful
  1. AlexZhu-MSFT 5,476 Reputation points Microsoft Vendor
    2021-10-26T02:45:53.143+00:00

    Hi,

    Here's a sample for your reference (you need to modify the command to cater for your real environment):

    1) create a file with .sh suffix (linux shell script) and give execute permission for the file
    143631-linux-command.png

    2) edit the file
    143600-linux-tar-command.png

    3) edit crontab to add a line to run the file daily
    for example, at 23:59 in each day, the shell script will run once (we can add more logic in the script file if requried)
    143641-linux-crontab.png

    Regards,

    Alex
    If the response is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

  2. arun kumar 71 Reputation points
    2021-10-26T06:28:37.157+00:00

    Hi Alex

    As per your answer all the files in the folder will get zipped. but i need only the files which are created on that particular day in that folder should get zipped and it should not zip all the files in that folder.

    the log files which are generated today should get zip to one zipped file, the log files which are going to generate tomorrow should get zipped to other zipped file tomorrow and should happen like this.

    Please help me with solution.

    Regards
    Arun

    0 comments No comments

  3. AlexZhu-MSFT 5,476 Reputation points Microsoft Vendor
    2021-10-26T13:19:28.73+00:00

    Hi,

    Try something like this (zipper.sh):

    #!/bin/bash  
    touch -t `date +%m%d0000` /tmp/$$  
    find . -type f -newer /tmp/$$ | xargs tar cvf /tmp/backup-"`date +"%d-%m-%Y"`".tar  
    rm /tmp/$$  
    

    144744-linux-find-xargs-command.png

    Regards,

    Alex
    If the response is helpful, please click "Accept Answer" and upvote it.


  4. arun kumar 71 Reputation points
    2021-11-03T02:42:46.097+00:00

    Hi Alex

    i also need one more help regarding above issue. if i run the script above you mentioned it will delete all the files in the folder. but i want to delete only the files which are zipped, can you please help me with that.

    Regards
    Arun

    0 comments No comments