Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

migrated from Mar 30 '10 at 17:35

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

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

Your Answer


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