Split an mbox into Messages with git mailsplit
git mailsplit takes one mbox or Maildir and turns it into one numbered file per message, ready for another program to chew through. By the end you will have files named 0001, 0002 and so on. This guide uses the git mailsplit command from Git 2.43.0, supplied here by the git-man package version 1:2.43.0-1ubuntu7.3.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about ten minutes. You need Git installed, a readable mbox or Maildir, and a separate output directory. The command writes new files, so check the destination before you run it. It needs no elevated privileges when you own the input and output paths.
1. Create an empty output directory
Choose a temporary or dedicated directory. Do not point -o at a directory holding files you need to keep: the generated names are predictable, and existing names may get overwritten.
mkdir -p ./split-mail
find ./split-mail -maxdepth 1 -type f -print
Expected output is empty. If find prints files, stop and pick another directory, or move those files somewhere safe first. This is an ordinary user command; sudo is not a fix for an unsafe destination.
2. Split an mbox file
Pass the mbox after the output option. The compact option syntax is part of this command's interface, so use -oDIRECTORY as shown:
$ git mailsplit -o./split-mail ./inbox.mbox
2
The number printed is the count of messages written. This example creates ./split-mail/0001 and ./split-mail/0002. Check both the names and the first lines before handing them to whatever comes next:
find ./split-mail -maxdepth 1 -type f -printf '%f\n' | sort
sed -n '1,8p' ./split-mail/0001
Checkpoint
You should see four-digit names with leading zeroes, and the first file should hold the first message's headers. That numbering is not a message identifier from the mail system, just a sequence Git invented.
3. Read an mbox from standard input
Omit the input path and git mailsplit reads the mbox from standard input instead, which is handy when another command produces an mbox stream. Keep the output directory check from step 1 either way:
$ git mailsplit -o./split-mail < ./inbox.mbox
2
Do not combine an input redirection with a second mbox argument unless you genuinely want two inputs at once. For a repeatable job, record the exit status straight after the command:
git mailsplit -o./split-mail < ./inbox.mbox
status=$?
printf 'git-mailsplit status: %s\n' "$status"
test "$status" -eq 0
A zero status means the split completed. Non-zero means the output needs a look before anything processes it: the command may have written some files before the error, so retry into a fresh empty directory rather than mixing two attempts together.
4. Split a Maildir
A Maildir is a directory containing cur, tmp and new subdirectories. Pass its root instead of an mbox file:
$ git mailsplit -o./split-mail ./Maildir
2
Maildir filenames are sorted to decide output order, which makes names and timestamps in the source directory operational data rather than decoration. If order matters here, check the source names and test the resulting sequence before you apply patches or import messages from it.
Checkpoint
Verify the count and check a representative output file:
find ./split-mail -maxdepth 1 -type f -printf '%f\n' | sort
sed -n '1,12p' ./split-mail/0001
5. Handle the format options deliberately
- -b accepts a file that does not begin with a
Fromline as one complete message instead of rejecting it. This relaxes input checking, so do not reach for it just to silence an error you have not understood. - -fNN skips the first
NNnumbers;-f3starts at0004. -dN chooses a different filename precision. Both change names, not the order or content of messages, and are useful when your output has to fit into an existing numbering scheme. - --keep-cr keeps carriage returns at the end of CRLF lines, which are otherwise stripped.
- --mboxrd applies only to mboxrd input, where escaped
Fromlines need format-specific unescaping.
Picking the wrong format can change how message boundaries or body lines get interpreted, so confirm the source format before you choose.
6. Recover from a bad split
Stop downstream processing the moment the count looks wrong, a message starts in the wrong place, or the command exits non-zero. Keep the output as evidence, then retry into a new directory with the correct input option. Do not casually delete the only copy of a bad split: those files can be the easiest way to work out whether the source itself was malformed.
If the output is disposable and you have confirmed the source mbox or Maildir is intact, remove just that dedicated output directory with your normal file-management routine, recreate it, and run the command again. No service restart or elevated privilege needed for this recovery.
Done means
- Clean start: the output directory was empty before the split.
- Count matches: the reported count matches the messages you expected.
- Order correct: output files have the intended numbering and sorted order.
- Content verified: a representative file has the expected headers and body.
- Format flags justified: you chose
-b,--keep-cror--mboxrdonly because the input format required it.