Skip to content

Instantly share code, notes, and snippets.

@cognitom
Created September 18, 2021 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cognitom/4fb80710ff7a108a77f38a082c634044 to your computer and use it in GitHub Desktop.
Save cognitom/4fb80710ff7a108a77f38a082c634044 to your computer and use it in GitHub Desktop.
In some cases, I got to get the user/group's name from its id. Here's how-to.
# provide some group's id
$gid=1000
# POSIX way
cat /etc/group | grep ":x:$gid:" | cut -d: -f1
# non-POSIX way which also handles LDAP
getent group "$gid" | cut -d: -f1
# Note that we can't use `id` command for this purpose.
# It always handles an argument as an user id, not group id.
# provide some user's id
$uid=1000
# POSIX way 1
cat /etc/passwd | grep ":x:$uid:" | cut -d: -f1
# POSIX way 2, but sometimes doesn't work
id -un "$uid"
# non-POSIX way which also handles LDAP
getent passwd "$uid" | cut -d: -f1
# See also https://unix.stackexchange.com/questions/36580/how-can-i-look-up-a-username-by-id-in-linux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment