Created
September 18, 2021 01:56
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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