Listing all the groups to which a user belongs in macOS

I want to know the groups of some user.

$dscl . list /groups filterByUSer theFindUser

Is this possible (even if I have to enumerate all the groups and then filter by user) in a straightforward manner?


Try the command given below.

id -Gn [user]

On my Mac, the output from id -Gn davidanderson is given below.

staff everyone localaccounts _appserverusr admin _appserveradm _lpadmin _appstore _lpoperator _developer _analyticsusers com.apple.access_ftp com.apple.access_screensharing com.apple.access_ssh 2 1

The delimiter could be changed from a space to a comma by entering the following.

groups="$(id -Gn davidanderson)"
groups="${groups// /,}"
echo "$groups"

Here, the output would be as follows.

staff,everyone,localaccounts,_appserverusr,admin,_appserveradm,_lpadmin,_appstore,_lpoperator,_developer,_analyticsusers,com.apple.access_ftp,com.apple.access_screensharing,com.apple.access_ssh,2,1

I am using High Sierra, macOs 10.13.4 and a bash shell.


To list all the groups to which a user belongs, type:

id [username]

[username] argument is optional. By default, the logged in user is assumed. The output will include the numeric user id uid, and the list of all the groups along with their group id gid, of which the user is member of. The first group in the output is the user's primary group.

To list just the group names type:

id -Gn [username]

To list just the group numbers type:

id -G [username]