3

I need to get and filter the linux users list like:

username1 username1_group
username2 username2_group
...
usernameN usernameN_group

I've tried, but only that I've found is:

cat /etc/passwd | grep /home | cut -d: -f1

It gives me the list of users in /home folder. But how can I add the group name to each of them?

4 Answers 4

4

Try this:

getent passwd | awk -F: '{print $1}' | while read name; do groups $name; done
2
  • 1
    Or just getent passwd | awk -F: '{print $1}' for a plain list of groups. Mar 30, 2010 at 17:52
  • 1
    @Andrejs: That lists users. I think you mean getent group... Mar 30, 2010 at 18:21
3
getent passwd

... and cut what you need.

Also, getent has the advantage that it will look at all databases on the system. /etc/passwd only contains information for local users but not other places like LDAP, NIS, etc.

2

The list of groups is /etc/group.

To get a list of the groups that a specific user is in, you can run

$ groups username
1

have a look at /etc/group

You must log in to answer this question.