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 findduplicate.sh

#!/bin/bash

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

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:

reza:x:1000:1000:Reza:/home/reza:/bin/bash
sadi:x:1001:1001:rnd:/home/sadi:/bin/bash
reza:x:1002:1002:Test:/home/reza12:/bin/zsh
awal:x:1005:1005:Awal:/home/awal:/bin/bash
turaj:x:1006:1006:Cloud:/home/turaj:/bin/bash
awal:x:1008:1008:Tech:/home/awal5:/sbin/nologin
reza:x:1009:1009:Reza2:/home/backup:/bin/sh

Running the script

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

# ./findduplicate.sh
Error: No input file detected

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

# ./findduplicate.sh 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.