aoakley.com

MarkAllRead - Linux Bash script to mark all maildir mail as read

This script marks all Maildir mail as read, including mail in folders. All mail in all folders will be marked as read.

I use this since my email is forwarded both to my home SMTP/IMAP server and to a webmail service. When I've finished reading the webmail, this script allows me to mark all mail on my server as read. There are fancier ways to sync particular webmail services with IMAP, but I don't need fancy. I just want to mark all mail in all folders as read.

Download
Bash script, text, <2k. Save to /usr/local/bin with chmod 755 and chown root.root .

Bugs: The script will ignore mail that you have forwarded or replied to before reading, or has been displayed in a preview window. This is pretty rare so I won't fix it, but if you want to email me an improvement, feel free. An earlier version posted to the Dovecot mailing list on 2009-01-09 ignored unread mail that had been moved to /cur/ folders, but this has been fixed.


#!/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

Public Domain - Andrew Oakley - 2009-01-11

Top - More Computing Articles - Article Index - aoakley.com