List users in all specified unix groups

groupsunixuser-managementusers

Is there an easy way to get a list of users who are in all specified groups?

For example, if I have the following users:

fred - rainbow, dell
jane - hp
zippy - rainbow, hp, dell
george - hp, dell
bungle - rainbow, hp, dell

I would like something like this:

[me@box ~]$ magic "dell,hp"
zippy, george, bungle

ie returning the users who are in both dell and hp.

If it is multiple steps that is fine, though if it's not really possible without having to resort to lots of Bash black magic, and it is quicker to do it in a RE-enabled text editor that is also fine.

I'm running on RHEL4 if that makes any difference.

Best Answer

I don't known any tool doing this but it is easy to script.

At first get the list of the users on the system, then run groups on each and at the end, grep on the desired groups :

getent passwd | sed 's/:.*$//g' | \
    while read user; do groups $user; done | \
    grep group1 | grep group2
Related Topic