Skip to content

Instantly share code, notes, and snippets.

@ericlathrop
Created February 19, 2013 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ericlathrop/4982676 to your computer and use it in GitHub Desktop.
Save ericlathrop/4982676 to your computer and use it in GitHub Desktop.
Convert MH mail folders to MBOX files, which are used by Mozilla Thunderbird.
#!/bin/bash
#
# mh2mbox.sh
# By Eric Lathrop <eric@ericlathrop.com>
#
# Convert MH mail folders to MBOX files
# Useful for switching to Mozilla Thunderbird
#
# Copy this script to ~/Mail/ and execute.
#
# Requires the "packf" command from the nmh package in Ubuntu
DEST="mbox"
# Note: in Ubuntu, packf gets installed under /usr/bin/mh/ so it's not normally in the $PATH
PACKF="/usr/bin/mh/packf"
(IFS='
'
for DIR in `find . -type d`; do
if [ "$DIR" = "." ]; then
continue
fi
if [ "$DIR" = "./$DEST" ]; then
continue
fi
CONVDIR="."
if [ `dirname $DIR` != "." ]; then
CONVDIR=`dirname "$DIR" | sed -e "s/\.\///" | sed -e "s/\//.sbd\//"`'.sbd'
fi
mkdir -p "$DEST/$CONVDIR"
MAILBOX=`basename $DIR`
MBOXPATH="$DEST/$CONVDIR/$MAILBOX"
# create an empty file in case there aren't any messages
# this fixes a thunderbird problem where it won't show subfolders
# for a folder that doesn't have a matching file
touch $MBOXPATH
yes | $PACKF +"$DIR" -mbox -file $MBOXPATH
done
)
@thegallus
Copy link

I have found some problems with folder tree deeper than 2 and with presence of spaces in folders name

Regard
gallus

@thegallus
Copy link

I have found the error.
CONVDIR=dirname "$DIR" | sed -e "s/.///" | sed -e "s///.sbd//"'.sbd'

You have to add g at the end of the second regular expression used by sed to obtain a "global" substitution, now the directories tree can be deep without limitation. The new code should be something like that:

CONVDIR=dirname "$DIR" | sed -e "s/.///" | sed -e "s///.sbd//g"'.sbd'

Regards
Alessandro "gallus" Gallieri

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment