Some notes on computer stuff

How to merge mailbox files of mbox format?

March 30, 2013
[howto] [mail] [mbox] [mutt]

This sounds like an easy task at first. But after looking on the Internet for a working and ready to be used solution, it turned out to be not so easy to find. There were some old scripts in python, but they didn't look very promising to me. So after spending some time on search this answer to my question was found.

It requires the following steps to be taken for each set of files that need to be merged:

  1. Merge two (or more) files in mbox format into one file.
  2. Open resulting file in mutt.
  3. Remove all duplicated entries.
  4. Save mailbox file.

The first step can be done with command like this

cat mbox2 >> mbox1

or

cat mbox1 mbox2 > mbox

Personally I would recommend the second one, because it preserves original files. In case you would like to use the first one, don't forget to make backup copies first.

The second step requires properly setup mutt, which should be pointed out the file you want to work with (you are using mutt for email, don't you?).

The third step is quite easy, you just need to open the mailbox and do this:

  1. Press : (colon) to enter command-line mode.
  2. Enter exec delete-pattern command and press enter key.
  3. Enter pattern ~=, which means all duplicated files and press enter key.

Or, if default D mapping is left untouched, just:

  1. Press D key to activate delete-pattern command.
  2. Enter pattern ~=, which means all duplicated files, and press enter key.

The last step is probably will be performed by mutt automatically on exit. Say yes when and if you will be prompted about whether messages marked for the deletion should be purged.

That's it. At this point merge is completed.

See also:

  • mutt user manual on exec command
  • man muttrc on ~= and other patterns