[Dovecot] Bash script to mark all mail read in Maildir + Dovecot
Hi, I'm writing a script to mark all new mail read, including all mail in folders, for my system which uses Maildir and Dovecot.
The reason for this script is that I use Google Mail at work, which forwards to my SMTP/IMAP system at home. When I've finished my last check of GMail at work, I want to be able to quickly mark all mail as read at home too.
Basically this script moves everything from all /new/ directories to /cur/ directories and appends :2,S ("Seen" info status) to the filename.
It seems to work, but I'm relatively new to IMAP and Maildir. Can anyone spot any potential problems? Thanks.
----cut here---
#!/bin/bash # markallread by Andrew Oakley www.aoakley.com Public Domain 2009-01-09 # A script to mark all MAILDIR mail as read, including folders # Assumes Dovecot folder naming dot prefix eg. ~/.maildir/.foldername
# Loop through ~/.maildir/.foldername/new/ directories
# This also does the ~/.maildir Inbox since it matches ~/.maildir/./new/
for i in ls -1 ~/.maildir/.*/new/*
do
# Replace /new/ with /cur/
# Also add status "seen" to message by appending :2,S to filename
# as per http://cr.yp.to/proto/maildir.html
mv $i echo $i | sed -r "s/^(.*)\/new\/(.*)$/\1\/cur\/\2:2,S/"
done
----end cut here---
-- Andrew Oakley andrew@aoakley.com
On Fri, 2009-01-09 at 16:26 +0000, Andrew Oakley wrote:
Hi, I'm writing a script to mark all new mail read, including all mail in folders, for my system which uses Maildir and Dovecot.
Another way would be:
printf "1 select inbox\n2 store 1:* +flags \\seen\n" | dovecot --exec-mail imap
-------- Original-Nachricht --------
Datum: Fri, 9 Jan 2009 16:26:49 +0000 Von: "Andrew Oakley" andrew@aoakley.com An: dovecot@dovecot.org Betreff: [Dovecot] Bash script to mark all mail read in Maildir + Dovecot
Hi, I'm writing a script to mark all new mail read, including all mail in folders, for my system which uses Maildir and Dovecot.
The reason for this script is that I use Google Mail at work, which forwards to my SMTP/IMAP system at home. When I've finished my last check of GMail at work, I want to be able to quickly mark all mail as read at home too.
Basically this script moves everything from all /new/ directories to /cur/ directories and appends :2,S ("Seen" info status) to the filename.
It seems to work, but I'm relatively new to IMAP and Maildir. Can anyone spot any potential problems? Thanks.
I spot a issue with folders having a space in them. Then your script would not work.
----cut here---
#!/bin/bash # markallread by Andrew Oakley www.aoakley.com Public Domain 2009-01-09 # A script to mark all MAILDIR mail as read, including folders # Assumes Dovecot folder naming dot prefix eg. ~/.maildir/.foldername
# Loop through ~/.maildir/.foldername/new/ directories # This also does the ~/.maildir Inbox since it matches ~/.maildir/./new/ for i in
ls -1 ~/.maildir/.*/new/*
do # Replace /new/ with /cur/ # Also add status "seen" to message by appending :2,S to filename # as per http://cr.yp.to/proto/maildir.html mv $iecho $i | sed -r "s/^(.*)\/new\/(.*)$/\1\/cur\/\2:2,S/"
done----end cut here---
Out of my head without testing, I would do it that way: ----cut here--- #!/bin/bash find ~/.maildir/ -type d -mindepth 2 -maxdepth 2 -name "new" | while read foo do cd "${foo}" && { for bar in * do mv -iv ${bar} ../cur/${bar}:2,S done } done ----end cut here---
-- Andrew Oakley andrew@aoakley.com
Steve
-- Psssst! Schon vom neuen GMX MultiMessenger gehört? Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger
- Andrew Oakley andrew@aoakley.com:
Hi, I'm writing a script to mark all new mail read, including all mail in folders, for my system which uses Maildir and Dovecot.
The reason for this script is that I use Google Mail at work, which forwards to my SMTP/IMAP system at home. When I've finished my last check of GMail at work, I want to be able to quickly mark all mail as read at home too.
Why not use imapsync to sync your home mailfolder with Google Mail. imapsync synchronizes message status too.
p@rick
-- state of mind Agentur für Kommunikation, Design und Softwareentwicklung
Patrick Koetter Tel: 089 45227227 Echinger Strasse 3 Fax: 089 45227226 85386 Eching Web: http://www.state-of-mind.de
Amtsgericht München Partnerschaftsregister PR 563
Following several responses from this list (thanks all for your comments), I've revised the script.
Notably, the script now copes with mail which has been moved to a /cur/ folder without being read. It does still ignore a few edge-case exceptions, in particular I'm not sure whether it copes in all situations with the "a" status on unread mail (which Thunderbird seems to add), but for my purposes it does the job.
I will be keeping the latest version updated with bugfixes at:
http://www.aoakley.com/articles/2009-01-11-markallread.php
...and would welcome more suggestions and new features. One of the first things that needs adding is variables to allow the maildir path and folder prefix to be configured.
#!/bin/bash # markallread by Andrew Oakley www.aoakley.com Public Domain 2009-01-11 # A script to mark all maildir mail as read, including folders # Assumptions: # * Your maildir folder is ~/.maildir # * Folders use Dovecot IMAP . prefix eg. ~/.maildir/.foldername # * Folder names do not contain spaces
# Loop through ~/.maildir/.foldername/new/ folders
# This also does the ~/.maildir Inbox since it matches ~/.maildir/./new/
for i in ls -1 ~/.maildir/.*/new/*
do
# Move from /new/ to /cur/
# Also add status "seen" to message by appending :2,S to filename
# as per http://cr.yp.to/proto/maildir.html
mv $i echo $i | sed -r "s/^(.*)\/new\/(.*)$/\1\/cur\/\2:2,S/"
done
# Loop through ~/.maildir/.foldername/cur/ folders
# Required in case new mail has been moved to a cur dir without reading
# Note how these already have :2, at the end of the filename
for i in ls -1 ~/.maildir/.*/cur/*:2, 2>/dev/null
do
# Add status "seen" to message by appending S to filename
mv $i echo $i | sed -r "s/^(.*)$/\1S/"
done
-- Andrew Oakley www.aoakley.com
participants (4)
-
Andrew Oakley
-
Patrick Ben Koetter
-
Steve
-
Timo Sirainen