summaryrefslogtreecommitdiff
path: root/find-mail-duplicates.sh
blob: c8ae0974bd2c894db207620d042dab745fc51abf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/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"