Problem in Getting members of a group using "grep" command

I wanted to see the members of a particular group and a person suggested me to do
"grep neighbor /etc/group" where neighbor is the group name.But when I did that it showed "neighbor:*:205:"
 So it didn't show any names?
 He also said I can do in a script as follows...
# Pass group name as an argument.
echo -n "Members of $1: "
grep $1 /etc/group| cut -d: -f4
   When I executed the above script from my home directory using "bash scriptname" it asked for "Members of :" and when I entered group name as neighbor, nothing happened ?

  So any ideas why that problem is occuring please?

Who is Participating?
jlevieConnect With a Mentor Commented:
> "grep neighbor /etc/group" where neighbor is the group name.But when I did that it showed "neighbor:*:205:"

Will only show the users that are members of that group if they've been explicitly listed as members in the group file, which isn't necessary and hasn't been done in this case. Various accounts may have their login group as 205 in /etc/password, which will make them members of that group. To find all accounts with a login group ID of 205 you can execute:

grep -e ".*:[0-9]*:205:.*" /etc/passwd
gopikrishAuthor Commented:
No actually someone mentioned one cool command which listed all member names of a particular group but I forgot that :(
  It was said by a member from Grex shell account.So if anyone knows that please share it here.Thanks.
cut -d: -f1 /etc/passwd |xargs -n 1 id |grep neighbor
A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

Line extracted from /etc/password:


This gives username:x means a shadowed password:userid:PRIMARY groupid::home directory:login shell

Line extracted from /etc/group:


This gives group name:x means shadowed password(man gpasswd):group id:comma seperated members of the group


also exists in /etc/group

...but because it requires access to some files that mail owns, it is made a member of the mail group as well.

Traditionally, there were circumstances where all users were made members of the users group, and so the approach that you were using (for the neighbor group ) would have worked in thos circumstances. Your approach would ahve got:

users:x:500:user1,user2,user3,user4 etc

Nowadays, due to heightened security concerns etc, whenever a new person is added, they get created as a default a brand new group for each person. It therefore requires a concious effort to enable them to have the same group privileges as someone else.


I suppose that I'd better mention that this appears to be the default approach for RH 8.0, before someone starts flaming....and there is no reason why everyone shouldn't have group 500 in the /etc/passwd file, instead of the

users:x:500:user1,user2,user3,user4 etc

in /etc/group

I guess it depends on the distribution?
Um, are you looking for something like the AIX command "lsgroup" then? There is work in progress on something along those lines in the GNU sysutils project ( ... and look at for (alpha grade) source)...
One could probably make some more or less intelligent script to get hols of and list group memberships (both primary and secondary), to somewhat emulate this functionality:-).

-- Glenn
gopikrishAuthor Commented:
Well, "members groupname" as well as
"cat groupname /etc/passwd | grep :205"
where 205 is the group name id
They both worked ! No one was able to mention that :) Anyway thanks for suggestions.
That doesn't actually solve the problem of displaying all the members of a group though.

/etc/passwd  gives login group only
/etc/groups   lists only additional group memberships of users

cat groupname /etc/passwd...   isn't right, that should give an error message about the file
"groupname" not existing

grep ':205'  /etc/passwd    would list login groups, but you can also get spurious entries, for
example if a user has UID 205 or realname 205  they will be shown.

awk -F: '{if ($4=="205"){print $0}}' /etc/passwd

Will show you all passwd file entries where GID is 205, and no spurious entries
gopikrishAuthor Commented:
I see.So can you explain what it does please? I dont understand that and also why 4 is used in that $4 and 0 is used in $0 ? Thanks.
In awk, $4 is the 4th field in the line (record) and $0 is the whole line
JUst curious is:


a part of any specifc distribution. I've never seen it before. Having said that, we all have our own way of doing things and perhaps mine jut doesn't need the members command?
Hm, "members"... Not usually part of any install I've seen recently, but I have this very very faint recollection of seeing some odd shell builtin like that some time.... long long time ago:-).

-- Glenn
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.