Поиск и удаление log файлов старше 3 дней : BASH

Если нужно найти log файлы и произвести подстыет занимаемого ими пространства можно воспользоваться скириптом на BASH


#!/bin/bash

# Replace '/path/to/files' with the appropriate path for your server
files_path='/path/to/files'

# Get today's date and the threshold date that is 3 days earlier
today=$(date +%s)
threshold=$(date -d "3 days ago" +%s)

# Find all files older than 3 days and calculate their total size
total_size=0
while IFS= read -r -d '' file; do
    mod_time=$(stat -c %Y "$file")
    if [[ $mod_time -le $threshold ]]; then
        size=$(stat -c %s "$file")
        total_size=$((total_size + size))
    fi
done < <(find "$files_path" -type f -mtime +2 -print0)

echo "Total size of files older than 3 days: $total_size bytes"



Следующий BASH скрипт удаляет все лог файлы страрше 3 дней

#!/bin/bash

# Replace '/var/log' with the appropriate path for your server
logs_path='/var/log'

# Get today's date and the threshold date that is 3 days earlier
today=$(date +%Y-%m-%d)
threshold=$(date -d "$today - 3 days" +%Y-%m-%d)

# Find all log files older than 3 days and delete them
find "$logs_path" -type f -name '*.log' -mtime +2 -exec rm {} \;