Mac OSX: users vs dscl command to list user

dscl . -read /Users/ doesn't print anything particularly interesting; you probably mean dscl . -ls /Users, which prints a list of user accounts that are defined on your computer (including a great many normally-hidden system accounts). users, on the other hand, prints a list of users who are currently logged in on your computer (usually just you).

For your second question: you can use the sudo command to run commands as another user (e.g. sudo -u _mysql mkdir mysqldir). Note that this requires admin rights, and will ask you to enter your password for verification.


Get a list of all users (just their short names):

dscl . -list /Users

Get detailed user info on a particular user:

dscl . -read /Users/<username>

Get a particular value in a user's info:

dscl . -read /Users/<username> <key>

Examples of <key> are RecordName, RealName, UniqueID, and NFSHomeDirectory.

Get detailed user info on all users:

dscl . -readall /Users

Get a particular value in all users' info:

dscl . -readall /Users <key>

Hopefully you notice the difference between -list and -read. Additional goodies: -plist outputs as XML, -search lets you specify a key and value and you will get output indicating where that value is.

2rs2ts:~/ $ dscl . -search /Users RealName "Andrew Garrett"          [12:04:07]
2rs2ts      RealName = (
    "Andrew Garrett"
)

2rs2ts:~/ $ dscl -plist . -read /Users/nobody                        [12:05:29]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>dsAttrTypeStandard:AppleMetaNodeLocation</key>
    <array>
        <string>/Local/Default</string>
    </array>
    <key>dsAttrTypeStandard:GeneratedUID</key>
    <array>
        <string>FFFFEEEE-DDDD-CCCC-BBBB-AAAAFFFFFFFE</string>
    </array>
    <key>dsAttrTypeStandard:NFSHomeDirectory</key>
    <array>
        <string>/var/empty</string>
    </array>
    <key>dsAttrTypeStandard:Password</key>
    <array>
        <string>*</string>
    </array>
    <key>dsAttrTypeStandard:PrimaryGroupID</key>
    <array>
        <string>-2</string>
    </array>
    <key>dsAttrTypeStandard:RealName</key>
    <array>
        <string>Unprivileged User</string>
    </array>
    <key>dsAttrTypeStandard:RecordName</key>
    <array>
        <string>nobody</string>
    </array>
    <key>dsAttrTypeStandard:RecordType</key>
    <array>
        <string>dsRecTypeStandard:Users</string>
    </array>
    <key>dsAttrTypeStandard:SMBRID</key>
    <array>
        <string>501</string>
    </array>
    <key>dsAttrTypeStandard:UniqueID</key>
    <array>
        <string>-2</string>
    </array>
    <key>dsAttrTypeStandard:UserShell</key>
    <array>
        <string>/usr/bin/false</string>
    </array>
</dict>
</plist>

Of course, you can read the man pages to get more info.


The 'users' command only lists the currently logged-in users.

DESCRIPTION users lists the login names of the users currently on the system, in sorted order, space separated, on a single line.