Last active
April 19, 2025 14:59
-
-
Save mikedigriz/34931e3d18b8c501a1651eac36495279 to your computer and use it in GitHub Desktop.
удаление логов с опорой на имя
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Очистка старых логов, принимает аргумент: | |
# $1 - директория ./dir1 (обязательный) | |
delete_old_logs() { | |
local directory=$1 | |
if [ -z "$1" ]; then | |
echo "Параметр directory в delete_old_logs() не задан" | |
exit 1 | |
fi | |
if [ ! -d "$1" ]; then | |
echo "Директория $1 не существует или указана неверно" | |
exit 1 | |
fi | |
# Текущая дата - 5 месяцев в формате YYYY-MM-DD | |
month_ago=$(date -d "5 months ago" +%Y-%m-%d) | |
for file in "$directory"/big-*.log; do | |
filedate=$(echo "$file" | grep -oP '(?<=big-)\d{4}-\d{2}-\d{2}') | |
if [[ "$filedate" < "$month_ago" ]]; then | |
echo "Удален $file" | |
fi | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Для импорта в запускаемый скрипт
. ./log_cleanup.sh
Для запуска в разных директориях
delete_old_logs ./dir1
delete_old_logs ./dir2