In conventional mbox
systems, the first message in /var/mail/you
is -- by definition -- unread and the oldest available.
formail -zxSubject: -1 -s </var/mail/you
To extract the message to a file (so you can conveniently forward just that message) and then extract the Subject:
header, try something like
tmp=$(maketemp -t findoldestunread.XXXXXXXX) || exit 127
trap 'rm -f $tmp' EXIT # remove temp file when done
trap 'exit 1' 1 2 3 5 15 # remove also in case of error / interruption
formail -1 -s </var/mail/you >"$tmp"
formail -zxSubject: <"$tmp"
sendmail -oi uncannyvictim@example.com <"$tmp"
In Maildir, the situation is somewhat more complex. New messages are temporarily stored in tmp
and then moved to cur
. The file name indicates flags; in so many words, look for a file name containing a comma near the end where S
is not among the characters after the comma.
find path/to/Maildir/cur -maxdepth 1 -type f \
-regex '.*,[^S,]*$' -printf '%C@ %p\n' |
sort | sed '1s/^[^ ]* //;q' | xargs formail -zxSubject:
The printf
format specifier prints the timestamp first for easy sorting. We sort, grab the first line, trim the timestamp, and feed it to formail
for extracting the Subject:
header. (This will obviously break if you have funky file names with newlines in them or something; there are ways around that, but I'm lazy and practical here.)
Refactoring to just print the file name should be trivial in this case -- just remove the pipe to xargs
and capture the output into a variable.
Some Maildir implementations will also have an index of some sort which might make this task a lot easier and faster than traversing the entire mailbox in the file system, but again, without more knowledge about which implementation you are using, this is just a speculative note at this point.
If your mailbox is not in either of these two formats, (you are weird and) you will need to update your question with more details.
Your reference to mail
implies that you are probably using a traditional Berkeley mbox
system, but there are many versions, some of which are somewhat esoteric.
procmail
is an actual command which should only be wrapped like I had fixed it:procmail
.[tag:procmail]
. For example: meta.stackexchange.com/questions/194690/remove-the-linked-tag