LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to list group members and show user id and full name (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-list-group-members-and-show-user-id-and-full-name-919081/)

bsalamon 12-16-2011 11:12 AM

How to list group members and show user id and full name
 
After finding this to show all users on a system I was wondering what modification would work to just show members of a given group, show their user id and full name?

awk -F":" '{ print "UserName: " $1 "\tUserID:" $3 "\tFullName: " $5 }' /etc/passwd

MensaWater 12-16-2011 12:29 PM

/etc/passwd contains the primary group of the user (the one specified with -g in useradd). If, however, the user is a member of secondary groups (specified with -G in useradd/usermod) then your script wouldn't find them. You'd need to add info to get info from /etc/group as well.

So your command would need to be modified to print the group field "$4" rather than the user field "$3" for primary:
awk -F":" '{ print "UserName: " $1 "\tUserID:" $4 "\tFullName: " $5 }' /etc/passwd

However as noted that wouldn't get secondary groups. You'd have to script that. The command "groups <username> will show you in which group names the specified user is a member. Also the "id <username>" will show you in which group numbers the specified user is a member.

You'd have to make a modified script that got user's from /etc/passwd then got the group information for them using your choice of groups or id.


All times are GMT -5. The time now is 06:29 PM.