35

How to get the group ID GID giving the group name.

The output would be for example:

Group adm with GID=4
47

Use the getent command for processing groups and user information, instead of manually reading /etc/passwd, /etc/groups etc. The system itself uses /etc/nsswitch.conf to decide where it gets its information from, and settings in the files may be overridden by other sources. getent obeys this configuration. getent prints data, no matter the source, in the same format as the files, so you can then parse the output the same way you would parse /etc/passwd:

getent group sudo | awk -F: '{printf "Group %s with GID=%d\n", $1, $3}'

Note that, for a username, this much more easier. Use id:

$ id -u lightdm
105
1
  • Ok, awk is shorter than perl =) – A.B. Jun 23 '15 at 16:05
28

This can be simply done with cut:

$ cut -d: -f3 < <(getent group sudo)
27

getent group sudo will get the line regarding sudo group from /etc/group file :

$ getent group sudo
sudo:x:27:foobar

Then we can just take the third field delimited by :.

If you want the output string accordingly, use command substitution within echo:

$ echo "Group sudo with GID="$(cut -d: -f3 < <(getent group sudo))""
Group sudo with GID=27
5
  • % echo "Group cdrom with GID="$(cut -d: -f3 < <(getent group sudo))"" Group cdrom with GID=27, please a little more generic =) – A.B. Jun 23 '15 at 16:07
  • 2
    Why the process substitution? What's wrong with GID="$(getent group cdrom | cut -d: -f3)"? – kos Jun 23 '15 at 16:24
  • @kos I don't like running things in subshells unless is a must..... – heemayl Jun 23 '15 at 17:08
  • @A.B. Edited... – heemayl Jun 23 '15 at 17:37
  • But it's POSIX...:'( No, just joking, I genuinely didn't know the difference. +1 – kos Jun 23 '15 at 18:05
2

A hack for the needed: (still maybe there is much better answer)

awk -F\: '{print "Group " $1 " with GID=" $3}' /etc/group | grep "group-name"

Simpler version(Thanks to @A.B):

awk -F\: '/sudo/ {print "Group " $1 " with GID=" $3}' /etc/group 

Example:

$ awk -F\: '{print "Group " $1 " with GID=" $3}' /etc/group | grep sudo 
Group sudo with GID=27
2
  • 1
    Try this: awk -F\: '/sudo/ {print "Group " $1 " with GID=" $3}' /etc/group – A.B. Jun 23 '15 at 15:51
  • See UUOC – kos Jun 23 '15 at 16:30
1

more complicated answers are always appreciated - i learn so much from reading them quite honestly.

I came upon this page because i was looking for the group id of my mail group, and unaware of where that information was stored.

your answers are great - here is the simple one i came up with

def@fourof4:/home$ cat /etc/group | grep mail

mail:x:8:

I know it does not sort the ID out, but it served my purpose.

Blockquote Try this: awk -F: '/sudo/ {print "Group " $1 " with GID=" $3}' /etc/group – A.B. Jun 23 '15 at 15:51 Blockquote

i see this also mentioned besides the one i quoted....everyone gets my love - sorry if i missed you

this is what guided me to that file.
Thanks and keep feeding me the excellent in any form you wish! def

2
  • This is risky because it also matches any other line in the file that contains the letters mail in that order. At least grep like this: grep "^mail:" /etc/group – Ulf Adams May 20 '20 at 10:49
  • noted - thanks for the new info! – def Feb 20 at 4:52
0

Using perl one-liner:

% perl -ne '@elements=(split /:/); printf "Group %s with GID=%s\n",$elements[0],$elements[2]' <<< $(getent group sudo)
Group sudo with GID=27

or shorter (and better)

% perl -F/:/ -ane 'printf "Group %s with GID=%s\n",$F[0],$F[2]' <<< $(getent group sudo)
Group sudo with GID=27
1
  • 2
    Pretty sure perl can do the splitting for you, I think the -F or -l option does that. – muru Jun 23 '15 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.