I've tried Googling but not getting anywhere. How can I list all the members of a group called
mygroup from the command-line in OS X?
dscl . list /groups will get me all the groups...but how can i see each group's members?
There's no standard command that lists all members of a group in OS X, so here's a shell function which does that:
Copy the above command-line to the Terminal, and then type
Some explanation for those who are interested:
There are five different ways (that I know of) that a user can be member of a group in OS X. The command
So rather than trying to keep track of all those, it seems like a better idea to simply check the membership of every user on the system (using dsmemberutil), and that's what the shell function and the script below do.
This members script is equivalent to the shell function, but has nicer handling of invalid input:
The five ways of being a group member are:
These can be explored with commands like
Example of 4: Membership of the Print Operator group __lpoperator_ is inherited by members of the Print Administrator group __lpadmin_, and membership of that group is inherited by the members of the admin group.
Example of 5:
You can use:
Note: This was my initial answer, written before I realized this answer still gives an incomplete result. (For example, it finds no members of the everyone group!) So I wrote a better answer, which includes a script that lists all members of a group in OS X.
mygroup's GroupMembership property can be printed with dscl like this:
But this isn't guaranteed to output all (or even any) of the group's members. What's missing are the users who are member of the group only by having it as their primary group ID.
A common example of this in OS X are regular login accounts, which have staff (group 20) as their primary group, but are not listed in the GroupMembership property of the staff group.
Those users can be found by searching for the numeric primary group ID (gid) like this example for the staff group (gid 20):
and the numeric gid (PrimaryGroupID) of mygroup is found by:
BE AWARE: The command above does not always display a complete list of ALL group members. E.g. for the group "staff" you only get "root" as a group member which is incomplete. To check it use one of the following commands as a default user (NOT "root"):
As a result you will see all groups your default logged user is a member of. One of them should be "staff". Thus, besides "root" there are more members of the group "staff", which are not listed by the command
The only reliable method to get really ALL members of a group in the terminal on OSX is already provided here by Arne Stenström. This is using his shell function resp. his shell script. Both works great!
You could use the following:
Here is an implementation for this problem which was derived from an implementation in a related discussion. The routine is somewhat generic, with a directory service lookup hook for any specific platform/architecture, so it can be used without modification on a heterogenous network. We've installed a symbolic link to this utility named
Similar to @duperuser's answer, the following will print out only the users of the group
Running the above command will produce something like this:
Next we pipe this text into
This gives us
Finally, we pass this into sed and replace the text