#!/bin/bash # Recursively find all files with parentheses in the name find . -type f -name '*(*' -print0 | while IFS= read -r -d '' file; do # Extract the date from the parentheses using awk date_in_parentheses=$(echo "$file" | awk -F'[()]' '{print $2}') # Convert the date to a format that can be compared formatted_date=$(date -d "$date_in_parentheses" +"%Y_%m_%d %H_%M_%S %Z" 2>/dev/null || echo "$date_in_parentheses") # Check if the formatted date is valid if [ -n "$formatted_date" ]; then # Create a temporary directory for duplicates temp_dir=$(mktemp -d) # Move all files with the same content inside parentheses to the temporary directory find . -type f -name "*($date_in_parentheses)" -exec mv -i {} "$temp_dir" \; # Find the newest file in the temporary directory newest_file=$(ls -t "$temp_dir" | head -n 1) # Move the newest file back to the original location if [ -n "$newest_file" ]; then mv -i "$temp_dir/$newest_file" "$file" echo "Kept: $file and removed older duplicates" fi # Remove the temporary directory rm -r "$temp_dir" else echo "Skipping $file due to invalid date format in parentheses" fi done