'list of logged in unique users in linux
I am working in linux environment as a non root user. I am using users command to get the logged in users
users
But it returns the user names multiple times because multiple shells are created with same login. Is there any way to print the unique user list using users commad. Even i tried by
users | sort -u
Still it returns the user names multiple times.
Solution 1:[1]
Try with this -
who| awk '{print $1}'|sort -u
Solution 2:[2]
users | sort -u
Still it returns the user names multiple times.
Of course. sort
is line based, and users
only prints a single line.
What you want is to just look at the first word per line before sort -u
in who
output:
$ who|cut -f 1 -d " "|sort -u
barney
fred
wilma
or
$ who|sed 's/ .*//' |sort -u
barney
fred
wilma
However, if you are interested in some of the actual lines output by who
you can also use
$ who|sort -u -k 1,1
barney pts/23 Aug 26 10:11 (:5.0)
fred pts/3 Jun 11 18:38 (:6.0)
wilma pts/0 Jul 31 07:29 (:3.0)
Solution 3:[3]
You can try this command: who | cut -d' ' -f1 | sort | uniq
You can use w
command to get the list of logged in users and the details
Solution 4:[4]
This one is a bit shorter:
users | tr ' ' '\n' | sort -u
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ratednitesh |
Solution 2 | |
Solution 3 | Mahi |
Solution 4 | Tim |