Preciso excluir arquivos JPG e jpg com base nas seguintes condições
A pasta tem vários arquivos JPG e jpg. Cada arquivo é nomeado, por exemplo: 172.30.165.212_20241231_132125.JPG
. Onde 172.30.165.212
é o endereço IP, 20241231
é a data no formato AAAAMMDD e 132125
é a hora no formato HHMMSS.
As condições de exclusão são:
1- O script deve sempre manter o arquivo mais recente por endereço IP com base em sua data/hora do nome do arquivo. Não importa quão antiga seja a data/hora.
2- Mas, como cada endereço IP pode ter vários arquivos, o script deve excluir todos os arquivos cuja data/hora no nome do arquivo seja mais de 2 horas mais antiga que a hora atual.
3- Nunca olhe para a data/hora de modificação do arquivo, apenas a encontrada no nome.
Tentei isso sem sucesso, pois os arquivos não são excluídos.
#!/bin/bash
# Define target directory
TARGET_DIR="/mnt/moe/results"
# Log start time
echo "[$(date)] Starting cleanup process in $TARGET_DIR"
# Function to process files for a single IP
process_ip_files() {
local ip_prefix=$1
local ip_files
# Find files matching the IP
ip_files=$(find "$TARGET_DIR" -type f -iname "${ip_prefix}_*" | sort)
# Skip if no files
if [[ -z "$ip_files" ]]; then
echo "No files found for IP: $ip_prefix"
return
fi
echo "Processing IP: $ip_prefix"
# Variables to track files and the most recent file
local most_recent_file=""
local most_recent_time=0
local files_to_delete=()
# Get current time in seconds since epoch
current_time=$(date +%s)
# Iterate over files to determine the most recent and deletion criteria
while IFS= read -r file; do
echo "Processing file: $file"
# Remove the path and get just the file name
base_file=$(basename "$file")
echo "Base file name: $base_file"
# Split file name into components
IFS='_' read -r ip date time ext <<< "$base_file"
# Validate the expected number of fields and format
if [[ -z "$ip" || -z "$date" || -z "$time" || "$ext" != "JPG" && "$ext" != "jpg" ]]; then
echo " Skipping file (does not match expected format): $file"
continue
fi
# Check the timestamp format (YYYYMMDD HHMMSS)
if ! [[ "$date" =~ ^[0-9]{8}$ ]] || ! [[ "$time" =~ ^[0-9]{6}$ ]]; then
echo " Skipping file (invalid timestamp format): $file"
continue
fi
# Convert to seconds since epoch
timestamp="$date $time"
file_time=$(date -d "$timestamp" +%s)
echo " File: $file"
echo " Timestamp: $timestamp"
echo " File time (epoch): $file_time"
echo " Current time (epoch): $current_time"
# Check if this file is the most recent one for the IP
if (( file_time > most_recent_time )); then
# If we already have a most recent file, we add it to the delete list
if [[ -n "$most_recent_file" ]]; then
files_to_delete+=("$most_recent_file")
fi
most_recent_file="$file"
most_recent_time="$file_time"
else
# Check if the file is older than 2 hours (7200 seconds)
if (( current_time - file_time > 7200 )); then
echo " Marking for deletion: $file"
files_to_delete+=("$file")
fi
fi
done <<< "$ip_files"
# Display the most recent file for this IP
echo "Most recent file for IP $ip_prefix: $most_recent_file"
# Deleting files not the most recent one
if [[ ${#files_to_delete[@]} -gt 0 ]]; then
echo "Files marked for deletion for IP $ip_prefix:"
for file in "${files_to_delete[@]}"; do
echo " - $file"
done
for file in "${files_to_delete[@]}"; do
if [[ "$file" != "$most_recent_file" ]]; then
echo "Deleting file: $file"
rm -v "$file"
fi
done
else
echo "No files to delete for IP $ip_prefix."
fi
}
# Process unique IP addresses
find "$TARGET_DIR" -type f \( -iname "*.jpg" -o -iname "*.JPG" \) -printf "%f\n" | \
awk -F'_' '{print $1}' | sort -u | while read -r ip; do
process_ip_files "$ip"
done
# Log completion
echo "[$(date)] Cleanup process finished."
Como exemplo, tenho os seguintes arquivos e a data/hora atual é 20241231 13:30
172.30.165.212_20241231_132125.JPG
172.30.165.212_20241231_122125.JPG
172.30.165.212_20241231_112125.JPG
172.30.165.212_20241231_102125.JPG
172.30.165.212_20241231_092125.JPG
172.30.165.213_20241231_062125.JPG
172.30.165.213_20241231_032125.JPG
172.30.165.213_20241231_012125.JPG
O script deve ser excluído
172.30.165.212_20241231_112125.JPG (older than 2 hours)
172.30.165.212_20241231_102125.JPG (older than 2 hours)
172.30.165.212_20241231_092125.JPG (older than 2 hours)
172.30.165.213_20241231_032125.JPG (older than 2 hours)
172.30.165.213_20241231_012125.JPG (older than 2 hours)
O script deve manter
172.30.165.212_20241231_132125.JPG (younger than 2 hours)
172.30.165.212_20241231_122125.JPG (younger than 2 hours)
172.30.165.213_20241231_062125.JPG (older than 2 hours but most recent from this ip address)