Linux Log Compression: 4 Steps to Save Disk Space

Quick Linux Tip #108:

Question: My logs are eating 50GB. How do I compress all old ones in one pass?

Try: sudo find /var/log -type f \( -name '*.log' -o -name '*.log.?' \) -mtime +7 ! -name '*.gz' -exec gzip -9 {} +

Info: Recursively finds and compresses log files older than 7 days using maximum compression (-9).

Examples:

  • $ sudo find /var/log -name '*.log' -mtime +30 -delete  # Delete log files older than 30 days
  • $ sudo logrotate -f /etc/logrotate.conf  # Force manual execution of logrotate
  • $ sudo journalctl --vacuum-size=1G  # Shrink systemd journal logs to a 1GB limit

Note: Text logs typically compress by 90%+. Always use logrotate or copy/truncate strategies for production logs rather than compressing active files directly.




LinuxTeck.com
linuxteck:~$ sudo find /var/log -type f \( -name '*.log' -o -name '*.log.?' \) -mtime +7 ! -name '*.gz' -exec gzip -9 {} +

linuxteck:~$ sudo du -sh /var/log
526G    /var/log

linuxteck:~$ sudo find /var/log -type f \( -name '*.log' -o -name '*.log.?' \) -mtime +7 ! -name '*.gz' -exec gzip -9 {} +

linuxteck:~$ sudo du -sh /var/log
4.8G    /var/log

linuxteck:~$ ls -la /var/log/nginx/
-rw-r----- 1 root adm  234K Oct 17 09:23 access.log
-rw-r----- 1 root adm  1.2M Oct 10 23:59 access.log.1.gz
-rw-r----- 1 root adm  1.5M Oct 03 23:59 access.log.2.gz
-rw-r----- 1 root adm  1.3M Sep 26 23:59 access.log.3.gz
linuxteck:~$

PREVIOUS ARTICLE Quick Linux Tip #107: Linux Secure Password Generation: 4 Easy Methods NEXT ARTICLE Quick Linux Tip #109: Linux Terminal Session Recording: 3 Easy Methods
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.