find number of groups assigned to a user

5,487

Solution 1

As groups returns a list of groups your account is in, you can use the following:

groups | wc -w

The wc -w will count the number of words returned by groups.

Solution 2

The groups command does not account for groups with spaces in its output. So a group with a space in the name is output the same as two separate groups.

I would do it this way:

 grep "apple users" /etc/group

This should return something like

 apple users:x:1010:fred,barney,dino
Share:
5,487
Cole Busby
Author by

Cole Busby

Updated on September 18, 2022

Comments

  • Cole Busby
    Cole Busby over 1 year

    Is there a way to find the number of groups assigned to a user in linux?

     as of right now i have:
     foo : foo bar apple users
    

    An app I use may have given a user a group of "apple users" but I cant tell and when i ran

     groups foo | awk {print $6}
    

    I received

     users
    

    I'm lost on how do find out how many users I have

  • Cole Busby
    Cole Busby almost 11 years
    Although I appreciate this answer, I had lookd in /etc/groups and "apple users" was not there. It is a group assigned by centrifydc. Thank you!