Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 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.

1  
This question appears to be off-topic because it is not a question. –  Dennis Kaarsemaker Jun 5 at 20:15
1  
@DennisKaarsemaker It is actually a question. –  Jacob Jun 5 at 20:22

1 Answer 1

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 num_email_accounts.sh

Copy and paste the script below.

#!/bin/bash

#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]}";
            output="$output\n$user\t$domain\t$count";
    fi
done

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

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

Run the script: bash ./num_email_accounts.sh

share|improve this answer

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