The who
command can be used to find logged-in users, but it prints duplicate values if there are multiple shells running. How do I get a list of the currently logged-in users without duplicates?
Command Line – How to List Logged-In Users Without Duplicates?
command lineuserswho
Best Answer
We can pipe the output of
who
toawk
to print only the first cell of each record (row) and then pipe it to the commandsort
, that will sort the values alphabetically and will output only the unique-u
entries:Or we can use only
awk
in this way:A POSIX compliant solution, provided by @dessert - where
cut
will use the spaces as delimiter-d' '
and will print only the first field of each record-f1
:Thanks to @DavidFoerster here is a lot shorter syntax that doesn't lose the information of all the other columns:
For the same purposes we could use the command
w
with the option-h
(ignore headers), for example:We could use also the command
users
combined with the commandrs
(reshape data) with the transpose option-T
and then againsort -u
:We could use and
who -q
with transposition in the following way - where the commandhead -1
will crop only the first line of the output of the previous command:See also:
How do I find who is logged-in as root?
How do I get the list of the active login sessions?