#!/usr/bin/env bash # find-mail-duplicates.sh # Usage: find-mail-duplicates.sh /path/to/maildir set -e # Check if a maildir path was provided if [ $# -lt 1 ]; then echo "Usage: $0 /path/to/maildir" echo "Example: $0 ~/Mail/ben@bensima.com/Trash" exit 1 fi MAILDIR="$1" # Validate the maildir if [ ! -d "$MAILDIR" ]; then echo "Error: Maildir '$MAILDIR' does not exist or is not a directory" exit 1 fi # Find all UIDs in the maildir TMP_UID_FILE=$(mktemp) find "$MAILDIR" -type f -name "*,U=*" | sed -E 's/.*,U=([0-9]+)[,:].*/\1/' | sort > "$TMP_UID_FILE" # Find duplicated UIDs DUPLICATE_UIDS=$(uniq -d "$TMP_UID_FILE") # Count duplicates DUPLICATE_COUNT=$(echo "$DUPLICATE_UIDS" | wc -l) if [ -z "$DUPLICATE_UIDS" ]; then echo "No duplicate UIDs found in $MAILDIR" rm "$TMP_UID_FILE" exit 0 fi # Process each duplicate UID for UID_NUM in $DUPLICATE_UIDS; do echo "$MAILDIR $UID_NUM" # echo "--------------------------------------------" # echo "Duplicate UID: $UID_NUM" # # Find messages with this UID # DUPLICATE_FILES=$(find "$MAILDIR" -type f -name "*,U=$UID_NUM[,:]*") # FILE_COUNT=$(echo "$DUPLICATE_FILES" | wc -l) # echo "Found $FILE_COUNT messages with UID $UID_NUM" # # List the duplicates # for f in $DUPLICATE_FILES; do # echo " $f" # done # echo "To fix these duplicates, run:" # echo " fix-mail-duplicates.sh \"$MAILDIR\" $UID_NUM" done rm "$TMP_UID_FILE"