How do I list all members of a group in Linux (and possibly other unices)?
getent group groupname | awk -F: '{print $4}' | tr , '\n'
This has 3 parts:
1 - getent group groupname
shows the line of the group in "/etc/group" file. Alternative to cat /etc/group | grep groupname
.
2 - awk
print's only the members in a single line separeted with ',' .
3 - tr
replace's ',' with a new line and print each user in a row.
4 - Optional: You can also use another pipe with sort
, if the users are too many.
Regards
There is a handy Debian and Ubuntu package called 'members' that provides this functionality:
Description: Shows the members of a group; by default, all members members is the complement of groups: whereas groups shows the groups a specified user belongs to, members shows users belonging to a specified group.
... You can ask for primary members, secondary members, both on one line, each on separate lines.