Sending attachments from the Linux command line using mail

An example of how to use the mail command in Linux to send file attachments.

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.

John Collins

I have been writing about web technology and software development since 2001. I am the developer of the Alpha Framework for PHP, and the Five.Today personal productivity app. I love open source, technology, and economics.