Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the command to get groupid of a group name in mac or linux? [closed]

How can I get group id of a group in mac os or linux?

ie., Command GroupName ==> should return the groupid

Eg:

Command staff   ==>   20
like image 833
Rajasekhar Avatar asked Jun 06 '12 07:06

Rajasekhar


1 Answers

On Linux, you can use getent(1):

$ getent group staff
staff:x:20:

If you only want 20:

$ getent group staff | cut -d: -f3
20

On OS X, you can use dscl(1):

$ dscl . -read /Groups/staff | awk '($1 == "PrimaryGroupID:") { print $2 }'
20

It can be easier to use this simple python command (using the grp library) to have the same result on both platforms:

$ python -c 'import grp; print grp.getgrnam("staff").gr_gid'
20
like image 175
math Avatar answered Oct 02 '22 12:10

math