Operations 6 min read

Automate Daily Log Compression on Linux with a Simple Bash Script and Cron

This guide shows how to create a Bash script that compresses and backs up yesterday's log files, removes the originals, and schedules the process with a daily cron job to prevent disk‑space exhaustion on Linux systems.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
Automate Daily Log Compression on Linux with a Simple Bash Script and Cron

Background

The system runs a front‑end service provided by a bank that generates extremely detailed logs without compression, producing roughly 5 GB of log data each day. When disk space ran low, a simple solution was needed to automate log compression, backup, and cleanup.

Implementation

Script creation

Create an executable script named backup.sh that performs the compression, backup, and deletion steps.

#!/bin/bash
cd /log/
echo '开始压缩...'
# Compress yesterday's log file into a tar.gz archive under the bak directory
tar -czvf /log/bak/trade.log.`date -d yesterday +%F`.tar.gz trade.log.`date -d yesterday +%F`
echo '压缩完毕...,执行删除'
# Remove the original uncompressed log file
rm -rf trade.log.`date -d yesterday +%F`
echo '删除完毕.'

The script changes to the /log/ directory, prints status messages with echo for debugging, uses tar -czvf to create a compressed archive whose name includes the date of the previous day (obtained via date -d yesterday +%F), and finally deletes the original log file with rm -rf.

The date -d yesterday +%F expression formats the date of the previous day as YYYY‑MM‑DD. Using the -d option allows retrieval of relative dates; without it, date +"%F" would return the current date.

Scheduling with cron

After making the script executable, schedule it with crontab so it runs automatically each day.

30 2 * * * /log/backup.sh

This cron entry means “run /log/backup.sh at 02:30 every day”. The five fields represent minute, hour, day of month, month, day of week, and the command respectively.

Typical crontab commands useful for managing the schedule are: crontab -e – edit the crontab file crontab -l – list current entries service crond status – check the cron daemon status service crond start|stop|restart|reload – control the daemon

Check mail spool for execution logs:

cd /var/spool/mail/username

Conclusion

The presented Bash script provides a lightweight, automated solution for daily log compression and cleanup on Linux, freeing up disk space and eliminating manual intervention. While the example is simple, it can be extended with additional features such as rotating backups, error handling, or integration with monitoring tools.

AutomationLinuxcronbashlog management
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.