Sending attachments from the Linux command line using mail

Published on 2012-09-12 by John Collins.

On occasion I find myself needing to quickly send a file to myself from a live system to another system, where I can then do some offline analysis (for example, on a log file or a network trace file from tcpdump).

If the file in question is small, you can achieve this quickly by simply emailing it to yourself via the mail command in Linux. Here is an example where I am emailing a .pcap file from a network trace to my email account:

$ mail -s 'Network trace test' -a /home/john/trace1.pcap

The -s flag is the subject line of the mail we are going to send, while the -a flag points to the file that we are going to attach (it's usually a good idea to compress these first if the file is large). When you run this command, it will wait for input: this input from you will become the body of the email. When you are ready, press ctrl-d to send it.

Note that to use this command, you will need to have Sendmail or Postfix installed locally to send the outgoing mail.

Updated 2022 : note that the above post was originally published in 2012, but is left here for archival purposes. The steps above have not been tested recently, so may be outdated.