#!/usr/bin/perl -w use strict; use constant MAIL_ALIASES => "/etc/mail/aliases"; my $site = shift; # all users of a site have home directories under # /home/sites/XXX/users/. my @users = (map { m{/([^/]*)$} } # `basename' with a RE (grep { -d $_ } # only directories # glob )); # build a big regular expression with each user name; # use \b to anchor the pattern match, so that a user called # `jane' won't show up entries for `janet' my $pattern = '\b(' . join("|", @users) . ')\b'; open ALIASES, MAIL_ALIASES or die("Failed to open ".MAIL_ALIASES."; $!"); while () { print if m/$pattern/o; } close ALIASES;