When working with Linux system as a administrator, may need to get a list of all groups in the system or to count the number of groups. This tutorial explains how to do that.
Get list of groups
/etc/group is a text file which stores details about groups in Linux system. File can be viewed with
An example of an
root:x:0: daemon:x:1: bin:x:2: sys:x:3: adm:x:4:syslog,john ...... systemd-network:x:102: systemd-resolve:x:103: systemd-timesync:x:104: crontab:x:105: ......
Each line contains information of one group which has 4 fields separated by colons (
- Group name (e.g.
- Encrypted password (
xrepresents that the password located in the
- GID - group ID number (e.g.
- Username list - a list of users who are members of the group (e.g.
Check if group exists
Various operations can be performed with
/etc/group file. We can check whether a group exists in Linux system using the following command:
cat /etc/group | grep -w adm
If the group exists, the command will print group information. No output means that group doesn't exist.
Get only group names
cut command we can print only the first field which contains the group name:
cut -d: -f1 /etc/group
Get the number of groups
wc command can print the number of groups in the system as follows:
cat /etc/group | wc -l
Leave a CommentCancel reply