Compress Old Linux Logs and Keep 30 Days

Quick Linux Tip #74:

Question: My logs are eating disk space. How do I compress old ones and keep only 30 days?

Try: find /var/log/app -name '*.log' -mtime +7 -exec gzip {} \; && find /var/log/app -name '*.log.gz' -mtime +30 -delete

Info: Compresses logs older than 7 days with gzip to save disk space, then deletes .log.gz archives older than 30 days to enforce retention.

Examples:

  • $ logrotate -f /etc/logrotate.conf  # Force run system log rotation
  • $ find /var/log -size +100M -exec truncate -s 0 {} \;  # Emergency: truncate huge log files
  • $ journalctl --vacuum-time=30d  # Purge systemd journal logs older than 30 days

Note: Use logrotate via /etc/logrotate.d/ for automated log management. For systemd journals, use --vacuum-size or --vacuum-time. The -mtime +7 test is based on whole 24-hour periods, so it can select files somewhat older than exactly 7 days.




LinuxTeck.com
linuxteck:~$ find /var/log/app -name '*.log' -mtime +7 -exec gzip {} \; && find /var/log/app -name '*.log.gz' -mtime +30 -delete

linuxteck:~$ ls -la /var/log/app/
-rw-r--r-- 1 root root  23456 Sep 18 10:23 current.log
-rw-r--r-- 1 root root 234567 Sep 17 23:59 app_2026-09-17.log
-rw-r--r-- 1 root root   3456 Sep 10 23:59 app_2026-09-10.log.gz
-rw-r--r-- 1 root root   4567 Sep 09 23:59 app_2026-09-09.log.gz

linuxteck:~$ du -sh /var/log/app
234M    /var/log/app
linuxteck:~$

PREVIOUS ARTICLE Quick Linux Tip #73: How to Trace Disk I/O Activity in Linux with btrace NEXT ARTICLE Quick Linux Tip #75: Pin Linux Processes to Specific CPUs with taskset
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.