Obtain Users and Groups from Mac

1

1

How would I obtain the Users and Groups listed under System Preferences from a Mac through the terminal?

I've tried

dscl . list /users
dscl . list /groups

But they give me a list of system-ish users as well. I just want the users and groups I would see if I went into System Preferences and viewed them. Any help would be much appreciated. Thanks in advance.

John

Posted 2012-10-04T14:37:51.453

Reputation: 181

Answers

3

It's a bit more complicated than this, but for the most part the Users & Groups preference pane only deals with users and groups with IDs above 500. You can emulate this by filtering the lists that dscl generates. Here's a quick & dirty version, using awk to do the filtering:

dscl . -readall /Users UniqueID | awk '/^RecordName:/ {name=$2}; /^UniqueID: / {if ($2 > 500) print name}'
dscl . -readall /Groups PrimaryGroupID | awk '/^PrimaryGroupID:/ {id=$2}; /^RecordName: / {if (id > 500) print $2}'

Gordon Davisson

Posted 2012-10-04T14:37:51.453

Reputation: 28 538

The first one worked, the second line didn't return anything at all not even Admin or Standard User as listed in System Preferences. – John – 2012-10-05T03:51:02.263

The second one is looking for groups, not users; if you haven't created any groups (other than the builtin system ones), it won't list anything. – Gordon Davisson – 2012-10-05T05:56:30.923

Thanks for the help. Is there any specific documentation related to this. You mentioned a quick and dirty version. Is there a better, perhaps shorter, more concise version? – John – 2012-10-06T03:45:04.977

I don't know of any documentation, and I'm not sure what additional complications (i.e. other criteria than ID number) there may be that this doesn't handle. A more complete version wouldn't be shorter, it'd probably be quite a bit longer. – Gordon Davisson – 2012-10-06T04:00:36.410

What would a more complete version do exactly? Thanks for all the information. – John – 2012-10-07T01:56:21.803

I don't know what else a more complete version might need to do; you'd probably need to get a look at the source code for the Users & Groups preference pane to find out exactly what criteria it uses to decide which accounts to show. – Gordon Davisson – 2012-10-07T17:57:55.933

If you have any more useful information related to this, it would be much appreciated. Otherwise, thank you for all the help. – John – 2012-10-07T22:01:18.740

Any idea how one would use this in objective c? I have something that reads commands such as system_profiler with 3 args, but not something that passes something this long. – John – 2012-10-08T00:11:30.423