users command in Linux Provides the names of users who are currently logged in.
The users
command in Linux is used to display a list of currently logged-in users on the system. It reads the /var/run/utmp
file to gather this information. Here are a few examples of how to use the users
command:
On the man page, describes it
- users – print the user names of users currently logged in to the current host.
man users – More details information.
users command in Linux with Examples
$ users
Count the number of logged-in users:
users | wc -w
By piping the output of the users
command to wc -w
, we can count the number of words in the output, which corresponds to the number of logged-in users.
Check if a specific user is logged in:
users | grep <username>
Check if a user is logged in and display a custom message:
if users | grep -q <username>; then echo "<username> is logged in"; else echo "<username> is not logged in"; fi
Conclusion
users users
command in Linux. The command is simple and primarily used to retrieve information about currently logged-in users. Thank you for reading the DevopsRoles page!