Linux¶
Get information of the
staffgroup.$ getent group staff staff:x:20:Get group ID of the
staffgroup.$ getent group staff | cut -d: -f3 20
Mac¶
Get information of the
staffgroup.$ dscl . -read /Groups/staffGet group ID of the
staffgroup.$ dscl . -read /Groups/staff | awk '($1 == "PrimaryGroupID:") { print $2 }' 20
As a matter of fact,
dscl in Mac is the equivalence of getent in Linux.
Both of them can be used to query user information as well.
Querying user information using
getent.getent passwd <uid>Querying user information using
dscl.dscl . -search /Users UniqueID <uid>
In both cases,
you then need to parse the output to get the username.
The output of getent is standard /etc/passwd format, something like this:
zamboni:x:1005:1005:Diego Zamboni,,,:/home/zamboni:/bin/bashThis is very easy to parse (using awk, for example) and gives you the full record at once.
dscl only provides the field you searched for, something like this:
zamboni UniqueID = (
501
)So if you want to get the full record, you would need to get the username and then query for it, like this:
dscl . -read /Users/zamboniThe output is harder to parse, in “keyword: value” form, but with many multiline values. You can also use the -plist option to get it in Apple’s plist format, which could be easier to parse.
Cross-platform Ways¶
You can also get group information using the grp module in Python.
import grp
print(grp.getgrnam("staff").gr_gid)