Skip to content

Task 7

You need to find and delete all log files older than a certain date.

Solution

To solve this problem, you can write a Bash script that uses the find command to locate files matching the criteria (in this case, log files older than a certain date) and the rm command to delete them.

Step 1: Write the Script

Create a new Bash script, let's call it delete_old_logs.sh, and add the following content:

#!/bin/bash

# Directory containing log files
log_directory="/path/to/log/directory"

# Check if the directory exists
if [ ! -d "$log_directory" ]; then
    echo "Error: Log directory '$log_directory' not found."
    exit 1
fi

# Define the threshold date (format: YYYY-MM-DD)
threshold_date="2022-12-31"

# Find and delete log files older than the threshold date
find "$log_directory" -type f -name "*.log" -mtime +1 -exec rm {} \;

Replace /path/to/log/directory with the path to the directory containing your log files. Modify the threshold_date variable to specify the cutoff date in the format YYYY-MM-DD.

This script uses the find command to locate .log files (-name "*.log") in the specified directory that are older than the threshold date (-mtime +1). The -exec option is used to execute the rm command on each matching file found.

Step 2: Make the Script Executable

Make the script executable using the following command:

chmod +x delete_old_logs.sh

Step 3: Run the Script

Execute the script to find and delete old log files:

./delete_old_logs.sh

Now, the script will locate and delete all log files older than the specified threshold date, helping you manage log files efficiently.

This solution automates the process of cleaning up old log files, freeing up disk space and keeping your system organized.