Check users in a Linux group

How do you check users in a linux group?

Replace groupname with the name of your group.

grep -i color groupname /etc/group

The command groups <user> will display the groups that user belongs to

id <user> will display groups id

The files /etc/passwd and /etc/group contain information concerning ids.

What is the color argument for? This doesn't really make sense.

grep -i color cop /etc/group
grep: cop: No such file or directory

I'm looking for all users in a particular linux group. I'm not trying to find the groups that a particular user is in.

On systems using traditional UNIX /etc/passwd and /etc/group to define the system's users and groups, the following should do what you want:

grp=Group_name
grep -E "[:,]$grp(,|$)" /etc/group | sed 's/:.*//'

If your system's grep utility doesn't understand the -E option, change grep -E to egrep .

Please ignore the above suggestion; it is backwards. It will give you the groups a user is in; not the users in a group.

That would not take into account the users whose primary or initial group is $grp , which is recorded in field 4 of /etc/passwd. Try

awk -vGRP="$grp"\
        'NR==FNR && $1==GRP     {printf "%s:%s", $1,$4; GRNO=$3; if ($4) DELIM=","}
         NR!=FNR && GRNO==""    {print "group not found!" > "/dev/stderr"; exit}
         NR!=FNR && $4==GRNO    {printf "%s%s", DELIM, $1; DELIM=","}
         END            {printf"\n"} 
        ' FS=: /etc/group /etc/passwd

It highlights the grep keyword in the output. You can remove the -i color if it is not working for you.

No, it does not! On systems that have a color option, you would get what you described using:

grep --color cop /etc/group

The command you suggested:

grep -i color cop /etc/group

performs a case insensitive search for the string color in the files cop and /etc/group .