Something that I have to do annoyingly often it seems is figure out what user accounts are associated with a particular group on Mac OS. I guess Mac probably makes this easy through the GUI, but I’m addicted to the command line, and most of the tutorials out there are for Linux. And you know, Mac seems to be a lot like Linux under the hood, but that impression is deceptive – it’s usually less like Linux than I think.
Anyway, it’s not too hard to google for this, but I thought I would add a page to the useful results.
Do it like this:
dscacheutil -q group | grep -A 4 gid
How it works:
dscacheutil is one of those Mac-only (or else Mac- and BSD-only) command line things for interacting with the OS. In this case, it’s information about the “Directory Service Cache.” Mkay? The -q parameter indicates that this is a query – so it will either return results, or else update the information stored in the DSCache. In this case, we’re querying for groups information. So far, this is more or less like typing groups at the command line in Linux.
But what if I’m only interested in the members of one group?
That’s what the pipe is for. You pipe the output of the dscacheutil command to grep to filter it for only what you need. The -A 4 tells grep that I’m interested not just in the matching line, but also in the three lines after the match. This is because the list of users, if available, is that many lines below the name of the group in the output. And then, of course, give it the name of the group you’re interested in. Done.
I was most recently curious who’s in “staff,” so my actual command was:
dscacheutil -q group | grep -A 4 staff