Reputation: 1462
I want to retrieve group id of a particular group name. Is there a command in Linux/UNIX systems to do this? Also if I want to do it the other way - get group name from group id, is that also possible?
Upvotes: 70
Views: 117659
Reputation: 3734
python -c "import grp; print(grp.getgrnam('groupname').gr_gid)"
This uses getgrnam
from
https://man7.org/linux/man-pages/man3/getgrnam.3.html.
Upvotes: 2
Reputation: 7012
Given the gid, here is how to get the group name:
getent group GID | cut -d: -f1
Given the group name, we get the gid:
getent group groupname | cut -d: -f3
UPDATE:
Instead of cut
a bash builtin can be used:
Example, get the group name for group ID 123.
groupid=123 IFS=: read GROUP_NAME REST <<<`getent group $groupid` echo $GROUP_NAME
Upvotes: 34
Reputation: 4180
You can use the following command:
awk -F\: '{print "Group " $1 " with GID=" $3}' /etc/group | grep "group-name"
or
cat /etc/group | grep group-name
where group-name
is your group to search.
Upvotes: 5