Monday, June 10, 2013

Linux/Unix mail - view and delete all mail for an user

Linux Command to view, delete mail:

After sudo as the user, to check the mail information for the user below command needs to be used:

Command 1: (view the unread mails)
$ mail
Output:
Mail version 8.1 6/6/93.  Type ? for help.

"/var/spool/mail/user1": 1 message 1 unread
>U  1 root@user1 Mon Jun 10 10:40  25/1044 ......

&

Note: In this case the "$" prompt is changed to "&" indicates inside mail process.

Command 2: (display unread mail content)
& <hit_return_or_enter_key>
Output:
Message 1:

From username@user1.com  Mon Jun 10 10:40:11 2013
Date: Mon, 10 Jun 2013 10:40:01 -0700
From: root@user1.com (Cron Daemon)
To: username@user1.com
Subject: Cron <username@user1> /startApp.ksh >> /logTest.log
Content-Type: text/plain; charset=UTF-8
.......
.......
&

Note: the above is the content of the unread mail in Linux. 
If there is no mail to read when you hit enter, in "&" prompt (in mail process), the output will be 
"At EOF"

Command 2 .1: (when there is no mail to read in mail process on hitting enter)
&
Output:
At EOF



Command 3: (delete mail options)
& d <hit_enter_or_return_key>
Output:

Note: Nothing will be displayed as output, back to & prompt. This command will delete the mail one at a time.

& d * <hit_enter_or_return_key>

Output:

Note: Nothing will be displayed as output, back to & prompt. This command will delete all the mail for that user.


Command 4: (display mail options)
& ? <hit_enter_or_return_key>

Output:

    Mail   Commands
t <message list>                type messages
n                               goto and type next message
e <message list>                edit messages
f <message list>                give head lines of messages
d <message list>                delete messages
s <message list> file           append messages to file
u <message list>                undelete messages
R <message list>                reply to message senders
r <message list>                reply to message senders and all recipients
pre <message list>              make messages go back to /usr/spool/mail
m <user list>                   mail to specific users
q                               quit, saving unresolved messages in mbox
x                               quit, do not remove system mailbox
h                               print out active message headers
!                               shell escape
cd [directory]                  chdir to directory or home if none given

A <message list> consists of integers, ranges of same, or user names separated
by spaces.  If omitted, Mail uses the last message typed.

A <user list> consists of user names or aliases separated by spaces.
Aliases are defined in .mailrc in your home directory.
&

Command 4: (to quit from the  mail process shell)
& q <hit_enter_or_return_key>

Output:

Note: Nothing will be displayed as output, back to "$" prompt.

After deleting all the mails or when there is not mails to read for the user:
Command 4:
$ mail
Output:
No mail for username

No comments:

Post a Comment