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?

Thanks in advance!

link|improve this question
feedback

migrated from stackoverflow.com Mar 30 '10 at 17:35

This question came from our site for professional and enthusiast programmers.

5 Answers

Try this:

getent passwd | awk -F: '{print $1}' | while read name; do groups $name; done
link|improve this answer
Or just getent passwd | awk -F: '{print $1}' for a plain list of groups. – Andrejs Cainikovs Mar 30 '10 at 17:52
1  
@Andrejs: That lists users. I think you mean getent group... – Dennis Williamson Mar 30 '10 at 18:21
feedback
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.

link|improve this answer
feedback

have a look at /etc/group

link|improve this answer
feedback

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
link|improve this answer
feedback

Thank you, guys, it's exactly what I've searching for:

getent passwd | grep /home | awk -F: '{print $1}' | while read name; do groups $name; done
link|improve this answer
You can link your accounts between SO and SF on your user page under the accounts tab. Then you can accept an answer. You should have probably posted this as an edit to your question. – Dennis Williamson Mar 30 '10 at 18:24
feedback

Your Answer

 
or
required, but never shown