Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Using normal bash tools (ie, built-ins or commonly-available command-line tools), is it

  • possible, and
  • how

to extract/save attachments on emails?

For example, say I have a nightly report which arrives via email but is a zip archive of several log files. I want to save all those zips into a backup directory. How would I accomplish that?

share|improve this question
"normal bash tools" -- do you mean using only the functions built into bash (i.e. what you'll find in the bash man page) or do you mean, more generally, command line tools which you could add to a bash shell script? – Doug Harris Sep 10 '10 at 17:46
@Doug Harris - either.. if I can call mail and do this, or save the attachment elsewise, that's fine too :) – warren Sep 10 '10 at 17:47
up vote 17 down vote accepted

If you're aiming for portability, beware that there are several different versions of mail(1) and mailx(1). There's a POSIX mailx command, but with very few requirements. And none of the implementations I have seem to parse attachments anyway.

You might have the mpack package. Its munpack command saves all parts of a MIME message into separate files, then all you have to do is save the interesting parts and clean up the rest.

There's also metamail. An equivalent of munpack is metamail -wy.

share|improve this answer

The best program for this purpose is ripmime.

It extracts the text and all attachments:

share|improve this answer
  • YES possible

  • This is HOW (Resource from here)

find dir containing files | while read file; do

create tempdir and copy file there

run munpack on file and copy attachments elsewhere

destroy tempdir (contents)


share|improve this answer
If anyone wants me to paste the Perl script here then just leave a comment – subanki Sep 10 '10 at 18:03
if you would be so kind (obviously leaving attribution of where you found it :) ) – warren Sep 10 '10 at 18:18
just read the script ... that's a heckuva lot of perl! – warren Sep 10 '10 at 18:24
so you are sure you want me to include that on my answer , it will become very long. – subanki Sep 10 '10 at 18:40
don't paste here, copying from the original source would be better in this case :) – warren Sep 28 '10 at 21:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .