List of all users and groups
The tool you want is almost certainly dscl. The shortest way to do it was already pointed out:
$ dscl . list /users$ dscl . list /groups
If you want to output information about each user, though, use readall
:
$ dscl . readall /users$ dscl . readall /groups
And if you need to programatically parse said information, use -plist to make your life easier:
$ dscl -plist . readall /users$ dscl -plist . readall /groups
Open Directory approach (from: http://rickcogley.blogspot.com/2008/11/listing-open-directory-users-on-os-x.html):
dscacheutil -q userdscacheutil -q group
Take each line from the respective output that starts with "name:" strip off the "name:" and you have your list.If you do not have dscacheutil, you can use the manual commands:
root# dscl localhost list /Local/Default/Usersroot# dscl localhost list /LDAPv3/127.0.0.1/Users
Old school approach for before Open Directory....(sigh):For list of users:
- Grab the /etc/passwd file from the system.
- Split it out by lines
- Split out each line based on ":"
- Take the first symbol for each line
For list of groups:
- Grab the /etc/group file from the system.
- Split it out by lines
- Split out each line based on ":"
- Take the first symbol for each line
Non-garbbled/no-tempfile commands:
# dscl . list /users# dscl . list /groups