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.
Türkçe: https://niyazi.net/tr/linux-isletim-sistemi-uzerinde-toplu-olarak-log-temizligi
There are none comment