Clear Ubuntu Server Logs

Clear Ubuntu Server Logs
Linux June 12, 2026 2 Views

#!/bin/bash
# Script to truncate log files in specified directories

# Corrected LOG_DIRS with proper quoting for each directory
LOG_DIRS=("/var/www/html" "/var/log" "/etc/script" "/var/www/vhost/dbbackup")  # Directories to search for log files
LOG_FILE="/var/scripts/truncate_log_files.log"  # Log file to record truncated log files

# Clear the log file if it already exists
> "$LOG_FILE"

for DIR in "${LOG_DIRS[@]}"; do
   # Find and truncate log files, logging their names
   find "$DIR" -type f -name "*.log" -exec sh -c 'sudo truncate -s 0 "$1" && echo "$1" >> "$0"' "$LOG_FILE" {} \;
done

echo "All specified logs truncated at $(date)" >> "$LOG_FILE"
 

Leave a Comment