User Tools

Site Tools


Split an Mbox file on the From Line

I've had to do this when you are using an mbox mail spool and you get an enormous mail (in my case usually some automated data import) which is too large to transfer in a reasonable period causing you to not be able to download any mail because there is a huge one in the way.

What you want to do is split the mbox file into individual files per email and then reassemble them without the large one. You might want to stop your mail server while you do this so your mbox file won't get added to by incoming mail, or you might want to do something more clever if you can't do that. Also turn off your mail client so it won't try to download mail.

So first you need to split the mbox file up:

csplit -n 4 filename /^From/ {*}

Look at the csplit man page to understand the options.

This gives me a file per email, named xx followed by a 4 digit number, so I rename the problem email according to it's size:

mv xx0123 bigmail

Move my existing mbox to something else in case anything goes wrong:

mv adam adam.old

Create a new mbox:

touch adam

Give it the right file ownerships for your mail server, for me this is:

chown adam:mail adam

Then put all of the new files together again in your mbox:

cat xx* >> adam

And remove the split files:

rm -f xx*

-f option stops Red Hat/Fedora asking me if I want to remove each file. You might want to omit this option.

You can now start your SMTP server again and download your mail. You might want to think about changing your mail server to use maildirs instead of mboxes if this happens frequently.

split_an_mbox_file_on_the_from_line.txt · Last modified: 2016/11/25 22:38 (external edit)