28 November 2012

sed to the rescue

Don't know how, but I managed to delete all the emails in my Thunderbird's inbox this morning: it was like I hit Ctrl + A and then Shift + Del, without noticing it. I only noticed there are no messages. The first thing to check was the Inbox file ~/.thunderbird/[random_chars\/Mail/Local Folders/Inbox. It was there and big (1.75GB) with readable (vi or such) content. OK, it should be recoverable.

What happens here: when you delete an email in Thunderbird (or SeaMonkey), it is not deleted completely, only the value of one of its headers, X-Mozilla-Status is changed. The actual content is removed only when you compact the folder. I used to have the mail client to automatically compact its folders when some space can be saved, but it feels like it start compacting (and freezing the app) always when you have work to do. So I disabled that.

So the first step was to investigate the status of the messages in my Inbox:

grep "X-Mozilla-Status:" Inbox | sort -u

The result was something like:

X-Mozilla-Status: 0009
X-Mozilla-Status: 000b
X-Mozilla-Status: 0019
X-Mozilla-Status: 001b
X-Mozilla-Status: 1009
X-Mozilla-Status: 100b
X-Mozilla-Status: 1019
X-Mozilla-Status: 101b

You need a bit of knowledge about those statuses: 0009 means "read and deleted", 000b means "read, replied and deleted" and so on.

So I had to parse the Inbox file and replace messages. Wile a tool like mboxgrep may make some things easier, sed is good enough. I created a statuses.txt file with the following content:

s/X-Mozilla-Status: 0009/X-Mozilla-Status: 0001/g
s/X-Mozilla-Status: 000b/X-Mozilla-Status: 0001/g
s/X-Mozilla-Status: 0019/X-Mozilla-Status: 0001/g
s/X-Mozilla-Status: 001b/X-Mozilla-Status: 0001/g
s/X-Mozilla-Status: 1009/X-Mozilla-Status: 0001/g
s/X-Mozilla-Status: 1009/X-Mozilla-Status: 0001/g
s/X-Mozilla-Status: 100b/X-Mozilla-Status: 0001/g
s/X-Mozilla-Status: 1019/X-Mozilla-Status: 0001/g
s/X-Mozilla-Status: 101b/X-Mozilla-Status: 0001/g

Then feed it to seed:

sed -f statuses.txt Inbox > Inbox1

Wait for a while and you have a new Inbox1 with all the messages undeleted (0001 is "read", if you want "unread", use 0000 instead). Delete the old Inbox and rename Inbox1 to Inbox. Open Thunderbird and everything works.


  1. LC_ALL=C sed -i '/^X-Mozilla-Status:/s/[01]0[01][9b]/0001/' Inbox

    1. yes, that's the shorter and less readable version :)

  2. How about something like:
    s/\(X-Mozilla-Status: ...\)9/\11/g
    s/\(X-Mozilla-Status: ...\)a/\12/g
    s/\(X-Mozilla-Status: ...\)f/\18/g

    (not tested)

    1. one can apply a lot of shortcuts, i hope the principle was understood so if/when someone has a similar problem it may be of help

  3. jc, why are you referring to sed as "seed"?