Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux - How to list all users and their UIDs

Tags:

linux

grep

awk

uid

How to write a script for linux which list all users from /etc/passwd and their UID

User1 uid=0001
User2 uid=0002
...

the script should use: grep, cut, id, for

like image 681
Tadeusz Majkowski Avatar asked Nov 28 '13 14:11

Tadeusz Majkowski


4 Answers

awk -F: '$0=$1 " uid="$3' /etc/passwd

awk is easier in this case.

-F defines field separator as :

so you want is 1st and 3rd colums. so build the $0 to provide your output format.

this is very basic usage of powerful awk. you may want to read some tutorials if you faced this kind of problem often.

This time you got fish, if I were you, I am gonna do some research on how to fishing.

like image 148
Kent Avatar answered Nov 01 '22 17:11

Kent


cut is nice for this:

cut -d: -f1 /etc/passwd

This means "cut, using : as the delimeter, everything except the first field from each line of the /etc/passwd file".

like image 24
Will Avatar answered Nov 01 '22 17:11

Will


I think best option is as that:
grep "/bin/bash" /etc/passwd | cut -d':' -f1

like image 24
Sanjar Stone Avatar answered Nov 01 '22 19:11

Sanjar Stone


awk  -F':' '$3>999 {print $1 " uid: " $3}' /etc/passwd | column -t | grep -v nobody

Gives you all regular users (uid >= 1000) neatly ordered including the userid.

like image 1
sjas Avatar answered Nov 01 '22 17:11

sjas