How to list all users in Linux - part 2

This is the second part of the series. You can review the first part here.

List only the user names using the /etc/passwd file

You can use the following awk command, to list only the usernames, without additional information:

$ awk -F':' '{ print $1}' /etc/passwd

Or, you can use the cut command:

$ cut -d: -f1 /etc/passwd

List user accounts without using the /etc/passwd file directly

Use the getent command to get a list of all users:

## similar output as for $ cat /etc/passwd command ##
$ getent passwd
## get a specific user - desiredUser ##
$ getent passwd | grep desiredUser 
## get a list of all users ##
$ getent passwd | cut -d: -f1
## count all user accounts using the wc ##
$ getent passwd | wc -l

You can use the compgen command with the -u option, to list only the user names:

$ compgen -u

If you want to learn more, read also the third part of this tutorial.

Other articles:

How to create users in Linux

How to delete users in Linux

How to create user groups in Linux

How to delete user groups in Linux