4

Every day I'm deleting log files in a folder.

find ~/folder -mtime +28 -type f -delete

Now I would like to archive and compress all files affected by above delete command and store this tar.gz in a new directory folder2.

So let's assume this:

~/folder:
  - x1 (3 days old)
  - x2 (3 days old)
  - y1 (29 days old)
  - y2 (29 days old)

~/folder2:
  - genericname1.tar.gz: (some already present archive)
    - ...

archive, compress and delete

~/folder:
  - x1
  - x2

~/folder2:
  - genericname1.tar.gz:
    - ...
  - genericname2.tar.gz:
    - y1
    - y2
  • 3
    That tool is called logrotate. (or just run tar ~/folder -mtime +28 -type f | xargs tar cvzf myarchive.tgz) logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large. – Michael D. Jan 16 '17 at 15:47
7

You can either run a script like this one (assuming GNU tar or libarchive bsdtar):

find . -type f -mtime +28 -print0 | tar -czvf backup.tar.gz --null -T -

Or start using logrotate (recommended). You place your custom configuration file in /etc/logrotate.d/yourfile, example file:

/var/log/news/news.crit {
    monthly
    rotate 2
    olddir /var/log/news/old
    missingok
    postrotate
    kill -HUP ‘cat /var/run/inn.pid‘
    endscript
    nocompress
}

You can see more information about all logrotate config options here

  • Doesn't this rotate all log files once a month, including the ones that are perhaps just 1 day old? – lfk May 1 '18 at 0:53
  • No, it doesn’t. It rotates the news.crit file used as example. – 13dimitar Jun 23 '18 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.