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?

  • "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

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.

  • Also to add that if you want to also extract the text MIME parts of the email when using munpack then use the '-t' option: munpack -t email_file – Pierz Oct 13 '17 at 10:07
  • Thanks for the info about metamail. Can you please help me up with this question? – george Mar 2 '18 at 12:59
  • Notably munpack works on single mails only. When dealing with a whole mailbox file, it needs to be split first. E.g. using formail < /val/mail/usersmailbox -s munpack . reference – FelixJN Mar 21 '20 at 21:53

The best program for this purpose is ripMIME.

It extracts the text and all attachments:


GitHub: inflex/ripMIME

  • 4
    sudo apt install ripmime works though. See also linux.die.net/man/1/ripmime . I use this program; works great, no dependencies except libc6. – unhammer Apr 25 '18 at 9:16
  • good stuff, metamail and munpack not available on Ubuntu but ripmime is and does exactly what expected. – Diego Jul 28 '18 at 14:51
  • 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)


  • If anyone wants me to paste the Perl script here then just leave a comment – subanki Sep 10 '10 at 18:03
  • 1
    if you would be so kind (obviously leaving attribution of where you found it :) ) – warren Sep 10 '10 at 18:18
  • 1
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.