How to use the command 'w' (with examples)
- Linux
- December 25, 2023
The ‘w’ command in Linux is used to display who is currently logged in and their processes. It provides information such as the username, terminal, login time, idle time, JCPU (time used by all processes attached to the terminal), PCPU (time used by the current process) and the command executed by the user.
Use case 1: Display information about all users who are currently logged in
Code:
w
Motivation: This use case is helpful to get an overview of all the users who are currently logged in to the system. It is useful for system administrators to keep track of the users and their activities.
Explanation: The command ‘w’ without any arguments displays information about all users who are currently logged in. It shows the username, terminal, login time, idle time, JCPU, PCPU, and the command executed by the user.
Example output:
16:37:09 up 1 day, 4:24, 4 users, load average: 0.00, 0.01, 0.05
USER TTY LOGIN@ IDLE JCPU PCPU WHAT
user1 tty1 15:43 2:47m 1.20s 0.38s -bash
user2 pts/0 15:56 0.00s 0.22s 0.05s vim test.txt
user3 pts/1 16:34 2:04 0.10s 0.10s -bash
user4 pts/2 16:37 0.00s 0.16s 0.00s w
Use case 2: Display information about a specific user
Code:
w username
Motivation: This use case allows you to display detailed information about a specific user who is currently logged in. It helps in monitoring the activity and processes of a particular user.
Explanation: By passing the username as an argument to the ‘w’ command, you can display information about that specific user. It shows details such as the username, terminal, login time, idle time, JCPU, PCPU, and the command executed by the user.
Example output:
16:34:29 up 1 day, 4:21, 4 users, load average: 0.00, 0.01, 0.05
USER TTY LOGIN@ IDLE JCPU PCPU WHAT
user3 pts/1 16:34 7:40s 0.10s 0.10s -bash
Use case 3: Display information without including the header
Code:
w --no-header
Motivation: This use case is helpful when you want to extract only the user information without the header. It is useful when automating or scripting tasks that require using the ‘w’ command.
Explanation: The ‘–no-header’ option is used to exclude the header from the output. It displays the user information directly without any headers.
Example output:
user3 pts/1 16:34 7:53m 0.10s 0.10s -bash
Use case 4: Display information without including the login, JCPU, and PCPU columns
Code:
w --short
Motivation: This use case is helpful when you only need basic information about the users, such as the username, terminal, login time, and idle time. It provides a more concise output.
Explanation: The ‘–short’ option is used to display a shortened version of the output, excluding the login, JCPU, and PCPU columns. It shows the username, terminal, login time, idle time, and the command executed by the user.
Example output:
16:43:41 up 1 day, 4:31, 4 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE WHAT
user1 tty1 :0 15:43 4:00m -bash
user2 pts/0 192.168.0.123 15:56 5.00s vim test.txt
user3 pts/1 192.168.0.123 16:34 1.00s -bash
user4 pts/2 192.168.0.123 16:37 3.00s w
Conclusion:
The ‘w’ command in Linux is a useful tool to display who is currently logged in and their processes. It allows you to gather information about users, their activities, and the resources being utilized. By using different options and arguments, you can customize the output according to your specific needs. Whether you want a detailed view of all users, information about a specific user, or a concise summary, the ‘w’ command provides flexibility and insight into user activity on your system.