script to move messages according to tags

Örjan Ekeberg ekeberg at kth.se
Wed Dec 11 04:58:10 PST 2019


Alan Schmitt <alan.schmitt at polytechnique.org> writes:
> Has someone written such a script that I could copy and 
> adapt?
>
> My use cases are:
> - find files with a deleted flag not in the Trash and move them to 
>   the
> Trash
> - find files with an archive flag in my active mail store and move 
>   them
> to my local mail store

Hi Alan,

Below is the script that I use which does more-or-less what you are
asking for.

The only "magic" here is the function safeMove which strips the
identifier number and flags from the filename when a file is moved.
These identifier numbers are added by mbsync which is the program I use
to synchronise my folders with our webmail server, and keeping the
numbers when moving the files proved to interfere with its
synchronisation logic.

Cheers,
Örjan

=== 8< ===
#!/bin/bash

MAILDIR=$HOME/MyMail

# Move a message file while removing its UID-part
function safeMove { s=${1##*/}; s=${s%%,*}; mv -f $1 $2/$s; }

# Move all deleted messages to the Trash folder
echo Moving $(notmuch count --output=files --exclude=false tag:deleted AND NOT folder:Trash) \
     deleted messages to the Trash folder
for i in $(notmuch search --exclude=false --output=files tag:deleted AND NOT folder:Trash); do
    safeMove $i ${MAILDIR}/Trash/cur
done

# Move all spam messages to the Spam folder
echo Moving $(notmuch count --output=files tag:spam AND NOT folder:Spam) \
     spam-marked messages to the Spam folder
for i in $(notmuch search --output=files tag:spam AND NOT folder:Spam); do
    safeMove $i ${MAILDIR}/Spam/cur
done

# Move all archived messages from Inbox to Archive folder
echo Moving $(notmuch count --output=files folder:Inbox AND NOT tag:inbox) \
     archived messages from Inbox to Archive folder
for i in $(notmuch search --output=files folder:Inbox AND NOT tag:inbox); do
    safeMove $i ${MAILDIR}/Arkiv/cur
done

echo Syncing with Webmail
mbsync twoway

echo Updating notmuch database
notmuch new --no-hooks
=== 8< ===


More information about the notmuch mailing list