ScaleScaleScaleScale

Tips / Nginx


How to Find all email accounts on a cPanel server

Yesterday one of my customers requested me to find all the email accounts created on a cPanel server. The best way to do this without using the cPanel API was to issue a simple ls command and filter the results with tr, grep and sed, it’s not the most clean way to do it but it was a very practical and fast solution:

ls -lah /home/*/mail/.* | grep @ | egrep -v 'home' | awk '{print $9}' | tr -d "." | sed -e 's/_/./' |  sed -e 's/_/./'

The output should be something like:

[root@myserver.com:~]ls -lah /home/*/mail/.* | grep @ | egrep -v 'home' | awk '{print $9}' | tr -d "." | sed -e 's/_/./' |  sed -e 's/_/./'
name2@site.com
namee@site1.com
name5@site1.com
name4@site2.com

How can I get the total number of email accounts created on cPanel?

Just send the output to a file and then pass wc -l command, example:

ls -lah /home/*/mail/.* | grep @ | egrep -v 'home' | awk '{print $9}' | tr -d "." | sed -e 's/_/./' |  sed -e 's/_/./' > /root/emailaccounts.txt
wc -l /root/emailaccounts.txt

Output:

[root@myserver.com:~]wc -l /root/emailaccounts.txt
969 /root/emailaccounts.txt

In case you need it, now you know how to find all the email accounts on a cPanel box. If you know other ways to do it, please don’t hesitate to share it using the comments form.

 

profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking