Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question is similar: Finding out the total number of email accounts in a cPanel server which I would love to comment on, but can't because of reputation.

This one-liner works well to get a total count of all email accounts.

for i in `cat /etc/localdomains`; do grep -E ^$i: /etc/userdomains ; done | sed 's/://g' | awk '{ system("cat /home/"$2"/etc/"$1"/passwd") }' 2>/dev/null | wc -l

How can I modify this to return a count per user account?

share|improve this question

closed as off-topic by Dennis Kaarsemaker, MadHatter, mdpc, dawud, squillman Jun 6 '14 at 19:50

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions must be relevant to professional system administration. Server Fault is dedicated to professional system and network administrators. End user and enthusiast questions are off-topic (contact your system administrator or hire a professional to help you out). Please see the Help Center for more information." – MadHatter, mdpc, dawud, squillman
If this question can be reworded to fit the rules in the help center, please edit the question.

This question appears to be off-topic because it is not a question. – Dennis Kaarsemaker Jun 5 '14 at 20:15
@DennisKaarsemaker It is actually a question. – Jacob Jun 5 '14 at 20:22
up vote 0 down vote accepted

Alright, this is really hacked together being my first bash script, but it works for what I needed.

Create a file: pico

Copy and paste the script below.


#Return count of email accounts per cPanel user

output="User\tDomain\t# Email Accounts\n\n------------------\t---------------------------------------\t--------------------";

for i in `cat /etc/localdomains`; do
    line=`grep -E ^$i: /etc/userdomains`;
    if [ ! -z "$line" -a "$line" != " " ]; then
            re="^([^-]+): (.*)$";
            [[ $line =~ $re ]] && domain="${BASH_REMATCH[1]}" && user="${BASH_REMATCH[2]}";
            wc="`wc -l "/home/"$user"/etc/"$domain"/passwd" 2>/dev/null`";
            re="^([^-]+) (.*)$";
            [[ $wc =~ $re ]] && count="${BASH_REMATCH[1]}";

echo -ne $output | column -s $'\t' -t;

Save and exit. Make the file executable: chmod +x

Run the script: bash ./

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.