UNIX / Linux Command To Check Existing Groups and Users

by on June 27, 2008 · 17 comments· LAST UPDATED June 27, 2008

in , ,

Q. How do I check the existing Linux / UNIX users and groups under Linux operating system?

A. You can easily check the existing users and groups under Linux using the following commands.

Find out if user exists in /etc/passwd file

/etc/passwd file stores essential information required during login. All you have to do is search this file for user name using following syntax:
$ egrep -i "^username" /etc/passwd
For, example find out if vivek user exists or not, enter:
$ egrep -i "^vivek" /etc/passwd
Sample output:

vivek:x:1000:1000:Vivek Gite,,,,:/home/vivek:/bin/bash

A quick shell script code:

# init
/bin/egrep  -i "^${USERID}" /etc/passwd
if [ $? -eq 0 ]; then
   echo "User $USERID exists in /etc/passwd"
   echo "User $USERID does not exists in /etc/passwd"
# ....

Normally, exit status is 0 returned if user accounts (lines) are found and 1 otherwise.

Find out if group exists in /etc/group file

/etc/group is an text file which defines the groups to which users belong under Linux and UNIX operating system. Again, you have to search /etc/group file using following syntax:
$ egrep -i "^groupname" /etc/group
For, example find out if vivek group exists or not, enter:
$ egrep -i "^vivek" /etc/group

id command

id is another command to display user / group information for any USERNAME, or the current user. To find out more about user called, tom, enter:
$ id tom
Sample output

id: tom: No such user

To find out ftpuser group, enter:
$ id -g ftpuser
Sample output:

id: ftpuser: No such user

id command exit status is 0 returned if user accounts (lines) are found and 1 otherwise. A sample shell script using id command:

/bin/id $USERID 2>/dev/null
[ $? -eq 0 ] && echo "User found" || echo "User not found"
/bin/id -g $USERID 2>/dev/null
[ $? -eq 0 ] && echo "Group found" || echo "Group not found"

Further readings:

  • Refer to id, passwd, group man pages
Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?

{ 17 comments… read them below or add one }

1 z0mbix June 27, 2008 at 12:15 pm

don’t forget the “:” after the username otherwise you could end up with this scenario:

$ egrep -i “^vivek” /etc/passwd
vivek:x:1000:1000:Vivek Gite,,,,:/home/vivek:/bin/bash
viveks:x:1001:1001:Vivek Smith,,,,:/home/viveks:/bin/bash


2 Ramesh | The Geek Stuff June 27, 2008 at 2:29 pm

If you are using NIS do the following:

ypcat passwd | grep vivek



3 Pedro June 27, 2008 at 4:00 pm

The ‘id’ command should be demonstrated first in this tutorial, as systems using LDAP (other or remote authentication services) will not have users in the local {passwd,group} files.

Also why the uses of egrep when a simple grep will do. Keep it simple for the beginners your aiming at.


4 davidhi June 27, 2008 at 6:26 pm

You should look at getent rather than grepping the local files. “getent passwd” or “getent group” will provide a unified view of users or groups available, respecting your NSS (Name Service Switch) configuration (which is important when you have additional users or groups via LDAP or NIS).


5 himadri August 9, 2008 at 6:45 pm

hey Vivek, that was cool..

many of us surely wont care if its grep or egrep ( or fgrep) as long as it does the job and we are taught these wonderful tricks..


6 New in linux July 15, 2009 at 9:08 am

Can you please tell me a command to list all of existing user ?


7 Shahin December 15, 2011 at 5:43 am

U can try
egrep “*” /etc/passwd
egrep “?” /etc/passwd


8 nixCraft July 15, 2009 at 9:09 am


cut -d: -f1 /etc/passwd | less


9 Praba March 17, 2010 at 4:00 am

Very nice site, I could get, what i want in seconds rather than in minutes


10 Rob November 14, 2011 at 11:22 am

`id` comand does not check if groups exist.
`man id`

Print user and group information for the specified USERNAME

the -g flag prints out the primary group id for the user


11 sohaib December 29, 2011 at 5:05 pm

have you find any solution for that?


12 sachin November 15, 2011 at 10:56 am

Linux Gurus,
Is there a Command to find out user creation date ?

or any other possible ways to find the same.

please help me
Its urgent.

Thanks In Advance


13 manh March 14, 2012 at 3:58 am

please tell everyone you ask.
no way to list the user is not disabled in linux.
and has been in how long dis.


14 Sebastian March 26, 2012 at 3:24 am

The grep approaches are all wrong. You are assuming that an user won’t pick a name that is a started substring of an existing group. Even worse, if you choose to limit the ‘username’ string you could match a group instead of a user. You will mistakenly get output from the script thinking that the user ‘apache’ (or whatever) exists…

You can’t play with strings without semantics. You need a tool that in fact *knows* that what you are talking about is indeed a user.

The best approach for not playing with strings semantics is the id command:

id – print real and effective user and group IDs



15 SMougenot August 1, 2012 at 10:29 am

As davidhi mentioned
Using getent is a much better solution in my opinion

# search for user named ‘vivek’
getent passwd vivek

#search for group named ‘vivek’
getent group vivek


16 shri May 24, 2014 at 1:17 pm

Thank you :-)


17 yash March 4, 2015 at 4:40 pm



Resolved my issue..!!!


Leave a Comment

Tagged as: , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: