M. Niyazi Alpay
M. Niyazi Alpay
M. Niyazi Alpay

I've been interested in computer systems since a very young age, and I've been programming since 2005. I have knowledge in PHP, MySQL, Python, MongoDB, and Linux.

 

about.me/Cryptograph

Batch Log Cleanup on Linux Operating System

Batch Log Cleanup on Linux Operating System

Linux operating systems keep records of errors, system warnings, and other events encountered during their operation. These logs can grow to large sizes over time, occupying significant disk space. While this may not be a concern for home users, web servers can face storage issues. Without proper maintenance, log files on servers can grow to substantial sizes.

In Linux, log files are located under /var/log. Deleting these log files entirely can harm the operating system. To prevent damage, it's necessary to empty the contents of these files. For example, running the command "rm -rf /var/log/maillog" to delete mail sending logs would cause harm because it removes the maillog file, preventing future logging. Instead, we can clear the contents of the log file using ":> /var/log/maillog". However, the /var/log directory contains many files and directories, which can be time-consuming to clear individually. You can expedite the process of clearing log files with the following commands, which should be saved with a ".sh" extension:

#!/bin/bash
# Muhammed Niyazi ALPAY
# https://niyazi.org
find /var/log/ -type f > logfiles.txt
while read line
do
    NAME=`echo "$line" | cut -d'.' -f1`
    EXTENSION=`echo "$line" | cut -d'.' -f2`
    rm -rf $NAME.gz
    :> "$line";
done < logfiles.txt
rm -rf logfiles.txt
rm -rf /var/log/*-2*
rm -rf /var/log/*.2*
echo "Log files have been cleared"

After saving these commands as logclear.sh, executing them will clear all log files in the /var/log directory and its subdirectories.

You may also want to read these

There are none comment

Leave a comment

Your email address will not be published. Required fields are marked *