DEV Community

Cover image for Linux Challenge #2: Real-World Log Cleanup with Command Line Automation
Muhammad Zulqernain Zahid
Muhammad Zulqernain Zahid

Posted on

Linux Challenge #2: Real-World Log Cleanup with Command Line Automation

Welcome back, command-line champs!

In this challenge, we’re simulating a real-world task every sysadmin faces: managing and cleaning up system logs. This is where your rm, mv, cp, find, and echo commands come into play in a real scenario!


Challenge Brief

You’ve been asked to clean up a log directory on a RHEL 9 machine.

The logs are eating up space, and you need to:

📝 Task Overview

  1. List all files in /var/logs/.
  2. Delete all .log files except syslog and auth.log.
  3. Move the remaining logs into an archive folder called /var/logs/archive/.
  4. Create a cleanup report that lists all deleted and moved files with timestamps.

Full Script Solution

#!/bin/bash

# Step 1: List all files in /var/logs/
echo "Listing all files in /var/logs/"
ls /var/logs/

# Step 2: Delete unnecessary log files except syslog and auth.log
echo "Deleting unnecessary log files..."
find /var/logs/ -maxdepth 1 -type f -name "*.log" ! -name "syslog" ! -name "auth.log" -exec rm -v {} \;

# Step 3: Move remaining .log files to an archive folder
echo "Moving important logs to archive..."
mkdir -p /var/logs/archive
find /var/logs/ -maxdepth 1 -type f -name "*.log" -exec mv {} /var/logs/archive/ \;

# Step 4: Create a cleanup report
echo "Cleanup report - $(date)" > /var/logs/cleanup_report.txt
echo "Moved files:" >> /var/logs/cleanup_report.txt
ls /var/logs/archive/ >> /var/logs/cleanup_report.txt
echo "Remaining files in /var/logs/:" >> /var/logs/cleanup_report.txt
ls /var/logs/ >> /var/logs/cleanup_report.txt
Enter fullscreen mode Exit fullscreen mode

What You Just Practiced

Command Purpose
find Searches for files matching specific criteria
rm -v Deletes files with verbose output
mv Moves files to another directory
mkdir -p Creates a directory (and parents if they don't exist)
echo > Outputs text to a file
ls Lists files for logging/reporting

Bonus Challenge

Make this script interactive:

  • Prompt the user for the log folder path.
  • Confirm before deleting files.
  • Add a log rotation option that backs up .log files with today’s date appended.

Want help building that version? Drop a comment and we’ll go there together.


Why This Matters

This type of script is gold in production. Automating file cleanup prevents your system from getting bloated and shows real command over Linux fundamentals. Try tweaking this to work for different folders or file types.


Next up? We’re jumping into file permissions and ownership. But for now—clean up, document your moves, and show that terminal who’s boss!


Tags:

#linux #redhat #rhcsa #techwithengineers #techtransition #learnlinux #devops #linuxlab #cloudwhistler #30daychallenge #opensource #techjourney

Top comments (1)

Collapse
 
nevodavid profile image
Nevo David

Nicely done, these are actually the kinds of small scripts I end up using way more than I thought I would lol.