Find duplicate users in /etc/passwd file on Linux

Yesterday, one of my colleagues requested if I can prepare a bash script that takes /etc/passwd file and finds duplicate users with their respective shell permission, given that the script would show error message if not executed in the given format <script_name> <input_file> .

I am not good at bash/shell scripting, but after couple of tries, the following one worked. Let’s say, the name of the script is


if [ ! $1 ] ; then
	echo "Error: No input file detected" ;
	exit 1
elif [ ! -e $1 ] ; then
	echo "Error: Invalid input detected" ;
	exit 1
cut -d: -f1 $1 | sort | uniq -d |
	while read -r username; do
  	awk -F: -vu="$username" '$1 == u { print $1, $7 }' $1

This script will take a file as an input and then purse through it to sort any duplicate first column (i.e. the usernames on /etc/passwd file) and then prints the first and seventh columns (user and shell). Similarly, I could print the username and UID by printing the first and third column as awk -F: -vu=”$username” ‘$1 == u { print $1, $3 }’ $1. You can print any of the seven columns just by putting comma separated numbers in the format as shown in the code.

For example, if I have a file named dummypasswd.txt with the following content:


Running the script

1. With no input file should give an error since no input is found:

# ./
Error: No input file detected

2. With wrong input file should also give an error:

# ./ nosuchfile.txt
Error: Invalid input detected

3. With valid input file in proper format should provide some results:

reza /bin/bash
reza /bin/zsh
reza /bin/sh
awal /bin/bash
awal /sbin/nologin

That pretty much served the purpose of what my colleague needed. But, just to see only the duplicate names, the following line in the last part of the script should do the job:

cut -d: -f1 $1 | sort | uniq -d |

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.