summaryrefslogtreecommitdiff
path: root/maildups.sh
blob: 2d1180cb2c5dca223f824ebf4abfd63dace90078 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash
# Copyright (C) 2011, 2013 Luke Shumaker <lukeshu@sbcglobal.net>

# Delete duplicate messages in $1 based on the Message-ID header

maildir=$1

msgid_file="$(mktemp --tmpdir "${0##*/}.XXXXXXXXXX")"
trap "rm -f -- $(printf '%q' "$msgid_file")" EXIT

echo -n 'Generating Message-ID table... '
grep -rH -m1 '^[Mm]essage-[Ii][Dd]:' "$maildir"| \
	sed -r 's/(.*):[Mm]essage-[Ii][Dd]:\s*(\S.*)/\2:\1/'| \
	sort -n>"$msgid_file"
echo 'done'

< "$msgid_file" sed 's/:.*//'|uniq -d|while read msgid; do
	echo "Removing dups of $msgid"
	< "$msgid_file" sed -n "s/^$msgid://p"|sed 1d|xargs -d'\n' rm -fv|sed 's/./  &/'
done