How to get the group ID GID giving the group name.
The output would be for example:
Group adm with GID=4
How to get the group ID GID giving the group name.
The output would be for example:
Group adm with GID=4
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
:Note that, for a username, this much more easier. Use
id
:This can be simply done with
cut
:getent group sudo
will get the line regardingsudo
group from/etc/group
file :Then we can just take the third field delimited by
:
.If you want the output string accordingly, use command substitution within
echo
:A hack for the needed: (still maybe there is much better answer)
Simpler version(Thanks to @A.B):
Example:
Using
perl
one-liner:or shorter (and better)