Bash Power Moves: A One-Liner for Automated Log Cleanup

Bash Power Moves: A One-Liner for Automated Log Cleanup

Bash Power Moves: A One-Liner for Automated Log Cleanup

 

Introduction: Log files are essential for debugging, monitoring, and auditing — but if left unchecked, they can easily balloon in size and consume disk space. This article demonstrates a simple yet powerful Bash one-liner to automate log cleanup, keeping your servers and workflows tidy. Whether you want to delete old logs or archive them for later analysis, this guide will teach you what’s happening under the hood and how to extend it safely.

1. Understanding the Problem: Log File Bloat

Most systems generate logs daily — application logs, server logs, and cron logs. Without regular cleanup, old logs pile up, which can cause storage bottlenecks and performance issues. A common lifecycle might retain logs for a set number of days, then either compress or delete them. Instead of manually running cleanup commands, **Bash automation** can handle this reliably.

For example, suppose all your logs live under /var/log/myapp/. Your goal: keep only the last 14 days of logs, and remove anything older.

Example:

find /var/log/myapp/ -type f -name '*.log' -mtime +14 -exec rm -f {} \;

This one-liner searches for all .log files older than 14 days and removes them. Simple yet incredibly effective.

2. Breaking Down the One-Liner

Let’s decode each part:

  • find /var/log/myapp/: the starting directory.
  • -type f: search for files only (ignore directories).
  • -name '*.log': match only log files.
  • -mtime +14: select files modified more than 14 days ago.
  • -exec rm -f {} \;: execute the remove command on each match.

Try running it with -print first to preview what will be deleted:

find /var/log/myapp/ -type f -name '*.log' -mtime +14 -print

This ensures you don’t accidentally delete something important.

3. Archiving Instead of Deleting

Sometimes you don’t want to delete old logs — you want to compress and archive them. Bash can handle that, too.

Example:

find /var/log/myapp/ -type f -name '*.log' -mtime +14 -exec gzip {} \;

This command compresses all logs older than 14 days in place. The .log files become .log.gz, saving disk space while retaining history. To move them into a dedicated archive folder, extend the command:

find /var/log/myapp/ -type f -name '*.log.gz' -mtime +14 -exec mv {} /var/log/myapp/archive/ \;

That subtle addition transforms the one-liner into an archival strategy.

4. Scheduling Automated Cleanup with Cron

To make it hands-free, set up a cron job. Open your crontab with:

crontab -e

Then add this line:

0 2 * * * find /var/log/myapp/ -type f -name '*.log' -mtime +14 -exec rm -f {} \;

This runs the log cleanup every day at 2 AM — during low server traffic. Cron ensures automation without having to rely on manual intervention. You can even capture cleanup summaries with:

0 2 * * * find /var/log/myapp/ -type f -name '*.log' -mtime +14 -print -exec rm -f {} \; >> /var/log/cleanup.log 2>&1

That way, you have a record of what’s deleted or compressed.

5. Performance and Safety Considerations

At scale, finding and deleting many files can be I/O-intensive. To optimize performance:

  • Use -delete instead of -exec rm to skip spawning new processes:
find /var/log/myapp/ -type f -name '*.log' -mtime +14 -delete
  • Use -maxdepth if you only want to search the top-level directory.
  • Combine find with xargs for faster bulk operations:
find /var/log/myapp/ -type f -name '*.log' -mtime +14 -print0 | xargs -0 rm -f

Always test your find expressions first using -print to avoid accidental data loss. For especially critical systems, consider running on a staging server first or backing up logs before deletion.

Conclusion

Cleaning up logs doesn’t have to be a tedious task. Bash’s find command offers a compact, powerful approach to automation. With a single line of code and a simple cron schedule, your system can stay lean, organized, and fast. Once you master this approach, you can adapt it for backups, temporary file cleanup, and more.

Pro tip: Store your cleanup commands in a version-controlled script so that your automation is easy to iterate on and audit. Small automation steps like these often yield big operational wins.

 

Useful links: