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

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.

How to install mail command on Linux

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.

kbd { display: inline-block;
margin: 0 .1em;
padding: .1em .6em;
font-family: Arial,”Helvetica Neue”,Helvetica,sans-serif;
font-size: 11px;
line-height: 1.4;
color: #242729;
text-shadow: 0 1px 0 #FFF;
background-color: #e1e3e5;
border: 1px solid #adb3b9;
border-radius: 3px;
box-shadow: 0 1px 0 rgba(12,13,14,0.2), 0 0 0 2px #FFF inset;
white-space: nowrap;}

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

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.