Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I find user name with only uid

Status
Not open for further replies.

Tison

Programmer
May 12, 1999
216
0
0
CH
I have the user id and I need a simple 1 line command to find the user loign.
eg I have uid 0
I need the login id (root) NOTE Not the users name or gecos.
 
grep :<uid>: /etc/passwd | cut -f1 -d:

>> grep &quot;:0:&quot; /etc/passwd| cut -f1 -d:
>> root

Alex
 
The only problem with alexhu's suggestion is that it will return anyone who has a group ID the same as the user ID you're looking for.

If you set $TARGETID to the uid you want, I think this will do the trick:

awk -F : &quot;\$3 ~ /^$TARGETID$/ { print \$1 }&quot; /etc/passwd

Actually I think uids can be shared, which seems like a bad idea to me, but you may want to account for the possibility of more than one result.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top