வியாழன், 3 ஜூலை, 2025
Help Unix scripts
#!/bin/bash
# Calculate date and time 1 month ago from current system date
cutoff_date=$(date -d "1 month ago" +%Y%m%d%H%M%S)
# Define the directory containing log files (modify path as needed)
log_directory="/home/log/SHELL_SCRIPTING"
# Define pattern for files with any prefix followed by 'YYYYMMDDHHMMSS' and optional suffix
file_pattern="*20[0-9][0-9][0-1][0-9][0-3][0-9][0-2][0-9][0-5][0-9][0-5][0-9]*"
# Get files matching the pattern
files=$(find "$log_directory" -maxdepth 1 -type f -name "$file_pattern")
for file in $files; do
# Extract date and time from filename (assumes format like any_prefix_YYYYMMDDHHMMSS_*)
if [[ $file =~ .*_([0-9]{14}) ]]; then
datetime_string=${BASH_REMATCH[1]}
# Parse the date and time from filename (YYYYMMDDHHMMSS)
file_datetime=$(date -d "$datetime_string" +%Y%m%d%H%M%S 2>/dev/null)
if [[ -n $file_datetime && $file_datetime -lt $cutoff_date ]]; then
# Delete the file
rm -f "$file"
echo "Deleted: $file"
fi
else
echo "Could not parse date and time from file: $(basename "$file")" >&2
fi
இதற்கு குழுசேர்:
கருத்துரைகளை இடு (Atom)
கருத்துகள் இல்லை:
கருத்துரையிடுக