Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question

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

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

5 Answers 5

Try this:

getent passwd | awk -F: '{print $1}' | while read name; do groups $name; done
share|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
@Andrejs: That lists users. I think you mean getent group... –  Dennis Williamson Mar 30 '10 at 18:21
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.

share|improve this answer

have a look at /etc/group

share|improve this answer

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
share|improve this answer

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
share|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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.