Skip to main content

Safe interactive log cleanup script

 You can use a safe interactive log cleanup script that:

  1. Scans /var/log for the largest files.

  2. Shows their sizes.

  3. Asks for confirmation before clearing each file.

  4. Clears only files you approve.

  5. Keeps the log files in place, so services can continue writing to them.

Script: cleanup_logs.sh

###################################################


#!/bin/bash

LOG_DIR="/var/log"
MIN_SIZE="+100M"

if [[ $EUID -ne 0 ]]; then
    echo "Please run this script as root."
    exit 1
fi

echo "========== Disk Usage Before =========="
df -h "$LOG_DIR"
du -sh "$LOG_DIR"

echo
echo "Scanning log files larger than 100 MB..."
echo

find "$LOG_DIR" -type f -size "$MIN_SIZE" -print0 |
while IFS= read -r -d '' file; do

    # Skip symlinks
    [[ -L "$file" ]] && continue

    size=$(du -h "$file" | cut -f1)

    echo "--------------------------------------"
    echo "File : $file"
    echo "Size : $size"

    read -r -p "Clear this file? (yes/no): " answer </dev/tty

    if [[ "$answer" == "yes" ]]; then
        if truncate -s 0 -- "$file"; then
            echo "CLEARED: $file"
        else
            echo "FAILED: $file"
        fi
    else
        echo "SKIPPED: $file"
    fi
done

echo
echo "========== Disk Usage After =========="
df -h "$LOG_DIR"
du -sh "$LOG_DIR"

echo "Cleanup completed."


#######################################

How to run

Save the script:

vi /root/cleanup_logs.sh

Make it executable:

chmod +x /root/cleanup_logs.sh

Run:

/root/cleanup_logs.sh

Example output

========== Disk Usage Before ==========
45G    /var/log

Scanning log files larger than 100 MB...

--------------------------------------
File : /var/log/messages
Size : 28G
Clear this file? (yes/no): yes
CLEARED: /var/log/messages

--------------------------------------
File : /var/log/secure
Size : 9G
Clear this file? (yes/no): no
SKIPPED: /var/log/secure

--------------------------------------
File : /var/log/httpd/access_log
Size : 850M
Clear this file? (yes/no): no
SKIPPED: /var/log/httpd/access_log

========== Disk Usage After ==========
17G    /var/log
Cleanup completed.

Important safety precautions

The script clears file contents permanently, not just old log entries. In particular, audit logs, security logs, and application logs may be needed for troubleshooting, retention requirements, or investigations.

For production servers, I recommend reviewing files first and using logrotate or journalctl --vacuum-size for managed logs where appropriate. This script requires typing the full word yes for every file; anything else skips it.






Comments

Popular posts from this blog

Install OpenVPN 2.4 and 2.6 on Alma / Rocky Linux

 To install OpenVPN on AlmaLinux 9.5, you can follow these steps: 1. Update the System : Before installing any software, it's a good idea to update your system: sudo dnf update -y 2. Install EPEL Repository : OpenVPN is available in the EPEL (Extra Packages for Enterprise Linux) repository. First, install the EPEL repository: sudo dnf install epel-release -y 3. Install OpenVPN : After enabling the EPEL repository, install OpenVPN: sudo dnf install openvpn -y 4. Install NetworkManager (Optional) : If you are using NetworkManager for managing network connections, you can install the OpenVPN plugin for NetworkManager: sudo dnf install NetworkManager-openvpn -y 5. Configure OpenVPN : Once installed, you need to configure OpenVPN by copying your .ovpn configuration file to the /etc/openvpn/ directory: sudo cp /path/to/your/config.ovpn /etc/openvpn/ 6. Start OpenVPN : You can start OpenVPN by specifying the configuration file directly: sudo openvpn --config /etc...

Vicidial Scratch installation Alma -9/10

Step 1 – Download the dependencies   hostnamectl set-hostname xxxxxx.xxxxx.xxx ### Use YOUR SubDomain vi /etc/hosts ##Change domain name for actual server ip (xxx.xxx.xxx.xxx   complete domain name    subdomain only) timedatectl set-timezone Asia/Kolkata yum check-update yum update -y yum -y install epel-release yum update -y yum install git -y yum install -y kernel* sudo dnf install kernel-devel-$(uname -r) -y # Install english language pack dnf install -y glibc-langpack-en # Configure new locale. This basically just edits /etc/locale.conf localectl set-locale en_US.UTF-8 #Disable SELINUX sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config     reboot Step 2 – Run the Script cd /usr/src/ git clone https://github.com/manish23k/vicidial-install-scripts cd vicidial-install-scripts ####For PHP 7.4 use this script. chmod +x alma-rocky9-ast16.sh ./alma-rocky9-ast16.sh Or the Asterisk 18 version: chmod +x alma-rocky9-ast18.sh ./alma-rock...

How to Format and Mount a New 1TB HDD

🖥 How to Format and Mount a New 1TB HDD to /var/www/recordings on Linux If you’ve added a new HDD to your Linux server and want to use it as a dedicated storage location — for example, to store Asterisk or web server recordings — you’ll need to partition, format, mount, and configure it to auto-mount at boot . In this guide, we’ll walk through the process of wiping the drive (careful — data will be lost!), creating a single ext4 partition , and mounting it permanently. Step 1: Identify Your New Disk Run: lsblk Example output: NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS sda 8 : 0 0 931.5 G 0 disk ├─sda1 8 : 1 0 50 M 0 part ├─sda2 8 : 2 0 540.8 G 0 part └─sda3 8 : 3 0 390.6 G 0 part In our case, /dev/sda is the new 1TB disk. ⚠ Warning: Double-check you’ve picked the right disk! Formatting the wrong one will destroy data. Step 2: Unmount Any Mounted Partitions sudo umount /dev/sda* 2>/dev/null Step 3: Wipe Old Partitions and Create One ...