The described functionality can be achieved using standard utilities:
for u in $(getent group | grep '^g1:' | cut -d: -f4 | tr , '\n'); do
printf "%s(uid=%d)\n" $u $(id -u "$u")
done
Update: the command:
getent passwd | grep -E '^([^:]+:){3}'$(getent group | grep '^g1:' | cut -d: -f3)':' | cut -d: -f1
will retrieve lines from /etc/passwd corresponding to users whose primary group is g1
. This can be combined with the previous command:
for u in $({ getent passwd | grep -E '^([^:]+:){3}'$(getent group | \
grep '^g1:' | cut -d: -f3)':' | cut -d: -f1; \
getent group | grep '^g1:' | cut -d: -f4 | tr , '\n'; }); do
printf "%s(uid=%d)\n" $u $(id -u "$u")
done | sort | uniq
with the added sorting and removal of duplicates at the end.
This command can be made into a shell function for convenience, using the group name as a parameter:
lid_replacement()
{
for u in $({ getent passwd | grep -E '^([^:]+:){3}'$(getent group | \
grep '^'$1':' | cut -d: -f3)':' | cut -d: -f1; \
getent group | grep '^'$1':' | cut -d: -f4 | tr , '\n'; }); do
printf "%s(uid=%d)\n" $u $(id -u "$u")
done | sort | uniq
}
call as: lid_replacement g1
Edit: Updated regex to match the exact group name.
Edit 2: Updated to use getent(1) and added the function lid_replacement
.
members
from the members package. The output format is very different fromlid
(it just prints all member usernames on one line separated by spaces, without uid= details) but, by default, it also lists both primary and secondary members of a group. The default is-a
or--all
to show all members on one line, and various options cause it print only primary (-p
,--primary
), only secondary (-s
,--secondary
), or primary and secondary members on separate lines (-t
or--two-lines
). – cas Jun 05 '22 at 06:30lid
/libuser-lid
, and the other answers cover other tools – muru Jun 05 '22 at 09:03