Maildir search
Views:
A dirty script for searching through mail boxes and copying mails to a shared (or other mail box).
#!/bin/bash
TARGET_FOLDER="/Path_to_Shared_Maildir";
PATTERNS=`cat <<END_OF_PATTERNS_CONFIGURATION
someone@example.com
a.nother@example.net
some phrase
a regular\s?//\s?expression
END_OF_PATTERNS_CONFIGURATION`;
# using this flakey script, cd in to the maildir you want to search
# It will recreate the mail box structure in the target mail dir
# then search for emails matching your patterns using OR logic
# and copy them to the target maildir.
#test -d "${TARGET_FOLDER}" || mkdir "${TARGET_FOLDER}"
#find . -type d -exec test ! -d "${TARGET_FOLDER}/"{} \; -exec mkdir "${TARGET_FOLDER}"/{} \;
find . -maxdepth 1 -name '.?*' -type d | sed -e 's/^\.\///g' | while read FOLDER;
do
test -d "${TARGET_FOLDER}/${FOLDER}" || maildirmake -f "${FOLDER}" "${TARGET_FOLDER}/";
done;
for PATTERN in `echo "${PATTERNS}"`;
do
echo "Looking for mails containing: ${PATTERN}";
for EMAIL in `grep -srlHiP "${PATTERN}" * .[^.]?*`;
do
echo "Copying: ${EMAIL}";
TARGET_EMAIL=`echo "${EMAIL}" | sed -e 's/^cur/\/cur/g'`;
TARGET_EMAIL=".${NAME}${TARGET_EMAIL}";
echo "Target: ${TARGET_FOLDER}/${TARGET_EMAIL}";
cp -p "${EMAIL}" "${TARGET_FOLDER}/${TARGET_EMAIL}";
touch -r "${EMAIL}" "${TARGET_FOLDER}/${TARGET_EMAIL}";
done;
