Need help with a shell script

w1z4rd

Karmic Sangoma
Joined
Jan 17, 2005
Messages
49,747
So I need to extract all the emails from one of the domains I have. There are roughly 2000 email accounts on this one domain. The server is a CentOS server with Cpanel loaded on it. I have two scripts to work with:

One using awk:

Code:
#!/bin/awk -f
#
# File: getAllEmail.awk
#
# This awk script takes the input of ls -d /home/*/mail/*/*
# checks to see if the directory exists, then outputs the email address. My example
# outputs them to a text file called emailaddress.txt
#
# I wrote it to get all the email addresses out of my cpanel box.
# You are free to use this program for your own purposes. If you
# improve it, please let me know. If it is horribly flawed, please let me know.
#
# Author: Hans C. Kaspersetz
# Date: 10/27/2007
# Email: hans [at] cyberxdesigns [dot] com
#
# Usage on cpaneli cli> ls -d /home/*/mail/*/*/ | getAllEmail.awk > emailaddress.txt

BEGIN {
}
{
tcmd = "test -d " $1
if(!system(tcmd)){
split($1,MyArray,"/")
print MyArray[6] "@" MyArray[5]
}
}

and one using PHP

Code:
#!/usr/local/bin/php
<?php
$folderlist = `ls -d /home/*/mail/*/*`;
$list_array = explode("\n",$folderlist);
foreach($list_array as $folder){
if(is_dir($folder)){
$folder_array = explode("/",$folder);
print $folder_array[5] . "@" . $folder_array[4] ."\n";
}
}
?>

Now these scripts work great on all domains, except my largest one. Where I get the following error:

-bash: /bin/ls: Argument list too long

Anyone know how to break any of the above scripts up so I can safely extract the emails from the domain?
 

ozoned

Well-Known Member
Joined
Mar 30, 2010
Messages
269
Change your command line usage, and run the awk script more than once. (how many depends on your usernames, and number of users):

Code:
usage:

ls -d /home/*/mail/[a-l]*/*/ | getAllEmail.awk >> emailaddress.txt

ls -d /home/*/mail/[m-z]*/*/ | getAllEmail.awk >> emailaddress.txt
 

GreGorGy

BULLSFAN
Joined
Jan 18, 2005
Messages
15,289
Change your command line usage, and run the awk script more than once. (how many depends on your usernames, and number of users):

Code:
usage:

ls -d /home/*/mail/[a-l]*/*/ | getAllEmail.awk >> emailaddress.txt

ls -d /home/*/mail/[m-z]*/*/ | getAllEmail.awk >> emailaddress.txt

That is sweet! ANd I know I will need to do the same sometime very soon so thanks.
 

w1z4rd

Karmic Sangoma
Joined
Jan 17, 2005
Messages
49,747
I used the find command in the following format... but it still have errors but it got me closer:

Code:
for i in `find /home/ -maxdepth 2 -type d -name mail`; do ls -d $i/*/* | getAllEmail.awk >> emailaddress.txt; done

However, with all the commands I will still having issue with the biggest domain, so using advice here and in other players, I was able to extract the big domains email accounts with the following commands:

Code:
 ls /home/domainname/mail/domainname.co.za/* -d | getAllEmail.awk >> email.txt

Thanks for the help :D
 

sin_x

Senior Member
Joined
Jan 26, 2007
Messages
542
icyrus is correct - find does not have the limitations that ls does.

simply:
find /home/*/mail/*/*/ -type d | getAllEmail.awk > emailaddress.txt

Will find all directories in the path, and pass to your awk script.
 
Top