Sep 14, 2018

Mail command in Linux

& ?         
               mail commands
type <message list>             type messages
next                            goto and type next message
from <message list>             give head lines of messages
headers                         print out active message headers
delete <message list>           delete messages
undelete <message list>         undelete messages
save <message list> folder      append messages to folder and mark as saved
copy <message list> folder      append messages to folder without marking them
write <message list> file       append message texts to file, save attachments
preserve <message list>         keep incoming messages in mailbox even if saved
Reply <message list>            reply to message senders
reply <message list>            reply to message senders and all recipients
mail addresses                  mail to specific recipients
file folder                     change to another folder
quit                            quit and apply changes to folder
xit                             quit and discard changes made to folder
!                               shell escape
cd <directory>                  chdir to directory or home if none given
list                            list names of all available commands

nMessage number n, such as 3.
.The current message (marked with '> ').
^The first undeleted message.
$The last message.
+The next undeleted message.
-The previous undeleted message.
*All messages.
n-mAn inclusive range of message numbers.
(username)All messages from (username).
/(string)All messages with (string) in the subject line (case ignored).

How to specify which message(s) you want a command to operate on, here are explanations of those basic commands:

(message number or shortcut)To view a message, just type its number; to view the last message, just type $; etc. A carriage return on an empty line means 'view the next message' — it's equivalent to n (or +).
hShows you a screenful of message headers (a "header" being the number, sender, date, size and subject).h with no message number shows the current screenful of messages (the number that make up a screenful is set with the screen variable, described below).
h$ shows you the last screenful of messages — which is usually what you're interested in (this is usually the first thing I type when I start mail).
h1 or h^ shows you the first screenful of messages.
z or z-If there is more than a screenful of messages, then z will show the next screenful, and z- will show the previous screenful.
dMark message(s) for deletion.d with no number marks the current message for deletion.
d with a number (or +-$, etc.) will mark the specified message(s) for deletion. To delete messages 1 to 3, you could do d 1-3, or d 1 2 3 (or d *, in this example where there are only 3 messages).
I said 'mark for deletion' instead of 'delete' because the changes you make are only saved when you type q.
uis 'undelete'. This just unmarks the message for deletion. You can do u followed by a number to undelete a specific message. But if you delete a message, and then quit with a q without having undeleted it, then that message really is gone.
q or xq quits and saves your changes; x quits without saving your changes. If you quit with an x, then any messages you d'ed will not actually be deleted the next time you invoke mail — also the little N for unread will still be there even if you read the message, etc. It's as if you were never there. This is sometimes a quick and dirty way to recover if you've deleted the wrong message, etc. — or if you aren't sure what you've done and just want to bail out.
mStart composing a message to a user or users, e.g. 'm fred terry sa2000'. Of course you can also send messages from the command line, rather than the mail prompt, using 'mail fred terry sa2000'.
ris used to reply to message — e.g. if you want to tell Fred 'Get well soon' in my example above, then you could do r 2.
sis used to save a message to a file. For example, if you want to save the 'Reminder' message above as 'reminder' in your current directory, you could use 's reminder'. Or 's /tmp/foo'> to save as /tmp/foo, etc.If the specified file already exists, the message you save will be appended. So, this makes a mail folder. I find folders invaluable. I have a directory called /home/kerl/mail; in it are about a dozen folders. This helps me organize things that I want to save, but that I don't want to have to see cluttering up my in-box everytime I read my mail. I can read a folder with mail -f (folder name) at the csh prompt, or f (folder name) at the mail prompt.
A common sequence of keystrokes for me is: Read a message; type 's /home/kerl/mail/eng' (or whatever folder); then 'd.'. There is no command to *move* a message to a folder, as there is in the graphical mailtool. Rather, you copy a message to a folder, then delete from you in-box, as two separate steps.
!can be used to get a shell — but ^Z is just as useful, if not moreso, since ^Z gets you back to the shell you invoked mail from, not a new one.
=just prints the current message number.
?prints a summary of commands that you can use at the mail prompt. This is a handy reminder.
vPuts the incoming message into vi; this may be a nicer way to read your mail than the PAGER (see below).