Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to organize all incoming email into the following directory structure based on the date of the email:

ROOT --+-- YYYYMMDD --+-- HH --+-- mm --+-- YYYYMMDD-HHmmSS-000001
       |              |        |        |
       |              |        |        |      ....
       |              |        |        |
       |              |        |        +-- YYYYMMDD-HHmmSS-NNNNNN
       |              |        +-- mm --
       |              +-- HH --+-- mm -- 

Note that each email will be stored as a separate file and the name of the file is YYYYMMDD-HHmmss-NNNNN, where NNNNN is a running number.

Can procmail or maildrop do this? If not, what other options are there?

Thanks in advance.

share|improve this question

I don't have an answer fulfilling all of what you want but a part of it. If it is sufficient to use the date at which the mail arrives instead of the date header of the mail itself and if one folder per date is also sufficient, you can do this with maildrop:

MAIL = "$HOME/Maildir"
DAY = `date +%d`
MONTH = `date +%m`
YEAR = `date +%y`
if ( $RETURNCODE != 0 )
`maildirmake -f $ARCHIVEFOLDER $MAIL`
# logfile "/tmp/maildrop"

I hope you can build up on this.

share|improve this answer

If you know some Python it should be a rather trivial exercise:

Python comes with an email package in the standardlib that allows your to read the emails from text files and automatically parse them into usable objects. After having parsed them you could just output them accordingly (each message has an "as_string" method).

The basic structure would be like this:

  1. Iterate over your files
  2. Open each file and parse into an email object
  3. take the date and split it into the fields you need
  4. open the target file handle (running number is last number+1 or 1)
  5. write msg.as_string() to the file
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.