Delete all root mail / inbox on Linux / Unix from a shell prompt

ADVERTISEMENTS



How to install mail command on Linux

I have CentOS Linux acting as a router for our small community-based college. I see logwatch and other cron job generating emails for the root account. How do I delete those emails? I don’t want to disable email facility but just wanted to get rid of all root emails. How do I delete root user mails (mailbox) file in Linux or Unix like system?The easiest way is to empty root or users email message file. The file is located at/var/spool/mail/root or /var/spool/mail/username location. You can read mail using mail/mailx command. It is an intelligent mail processing system, which has a command syntax reminiscent of ed with lines replaced by messages.

Type the following yum command on a CentOS/RHEL to install the same:

sudo yum install mailx

Use the following dnf command on a Fedora Linux to install it:

sudo dnf install mailx

Try the following apt command/apt-get command on an Ubuntu or Debian system:

sudo apt install mailutils

How do I read my mail messages from the CLI on Linux?

Simply type the following command:

mail

OR

mailx



The current message is initially the first message (numbered 1) and can be printed using the print command which can be abbreviated p. Just press p . The user can move among the messages much as he moves between lines in ed, with the commands + and - moving backwards and forwards, and simple numbers. Just type 10 to read email message # 10. After viewing a message the user can delete by pressing d . To reply press r .

How to delete root user’s mailbox/inbox file in Linux or Unix

Simply type the following command at shell prompt to delete all root mail:

> /var/spool/mail/root

Verify it with the following command or cat command or ls command:

mail

cat /var/spool/mail/root

ls -l /var/spool/mail/root



How to remove root user’s email every day using cron job

Simply run the following cron job:

@daily > /var/spool/mail/root