LinuxCommandLibrary

countmail

Count messages in mailboxes

SYNOPSIS

countmail [mailbox]

PARAMETERS

mailbox
    Optional path to mbox-format file. Defaults to $MAIL or user's spool (e.g., /var/mail/$USER).

DESCRIPTION

The countmail command is a lightweight utility designed to determine the number of email messages stored in a traditional Unix-style mailbox file (mbox format). It reads the specified mailbox file and counts the distinct messages by identifying separators typically beginning with 'From ' lines, which delimit individual emails in mbox files.

When invoked without arguments, countmail automatically uses the path from the MAIL environment variable or falls back to the user's default mail spool location, such as /var/mail/$USER or /var/spool/mail/$USER, depending on the system configuration. This makes it ideal for shell scripts, status bars (e.g., in i3blocks or polybar), or quick checks of unread mail counts.

It outputs a single integer representing the message count to standard output, enabling easy integration into scripts like echo "📧 $(countmail)". Note that it does not distinguish between read and unread messages; it simply tallies total messages. For modern Maildir or MH formats, alternative tools like dovecot scripts or mu are recommended.

Commonly found in BSD-derived systems (FreeBSD, NetBSD, OpenBSD) and some Linux distributions via ports or scripts, countmail exemplifies minimalist Unix philosophy: do one thing well with minimal dependencies.

CAVEATS

Supports only mbox(5) format; ignores Maildir, MH, or qmail. Counts total messages, not unread. May fail on locked or corrupted spools. No options; argument is positional.

ENVIRONMENT

MAIL: Overrides default mailbox path.
USER: Used to construct spool path if MAIL unset.

EXAMPLES

countmail
Prints count for default mailbox.

countmail ~/Mail/inbox
Counts messages in personal mbox.

newmail=$(countmail); [[ $newmail -gt 0 ]] && echo "New mail: $newmail"
Script usage check.

HISTORY

Originated in BSD Unix systems (4.4BSD era) as a simple mail-counting tool. Included in FreeBSD, NetBSD, OpenBSD base systems. Ported to Linux via user scripts or packages like bsdmainutils; evolved little due to its simplicity.

SEE ALSO

mail(1), mailx(1), biff(1), mbox(5), from(1)

Copied to clipboard