How to use the command 'w' (with examples)
- Osx
- December 25, 2023
The w
command in Unix-like operating systems is used to display information about currently logged-in users. It provides details such as user login, terminal device, remote host, login time, idle time, and the current processes being run by each user. This command is commonly used by system administrators to monitor user activity and manage system resources.
Use case 1: Show logged-in users information
Code:
w
Motivation:
This use case is useful when you want to quickly see a list of all logged-in users on the system and what they are currently doing. It helps administrators keep track of user activity and identify any unauthorized or suspicious activities.
Explanation:
The command w
without any arguments displays the information about the logged-in users. It provides details such as the username, terminal device (TTY), remote host from where the user logged in, the login time, idle time, and the current process being run by each user.
Example Output:
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
user1 pts/0 192.168.0.1 09:00 2:15m 0.05s 0.05s bash
user2 pts/1 192.168.0.2 09:30 1:45m 0.02s 0.02s vim myfile.txt
user3 pts/2 192.168.0.3 10:00 0.10s 0.01s 0.01s ls -l
In the example output above, there are three logged-in users: user1, user2, and user3. User1 is running the bash shell, user2 is editing a file using vim, and user3 is running the ls command to list files.
Use case 2: Show logged-in users information without a header
Code:
w -h
Motivation:
When displaying the output of the w
command in scripts or when parsing the output programmatically, it can be helpful to remove the header line. This makes it easier to process the data without having to deal with the header information.
Explanation:
The argument -h
is used with the w
command to suppress the header line in the output. When this argument is specified, the command only displays the information about the logged-in users, without the header line.
Example Output:
user1 pts/0 192.168.0.1 09:00 2:15m 0.05s 0.05s bash
user2 pts/1 192.168.0.2 09:30 1:45m 0.02s 0.02s vim myfile.txt
user3 pts/2 192.168.0.3 10:00 0.10s 0.01s 0.01s ls -l
In the example output above, the header line is not present, and only the information about the logged-in users is displayed.
Use case 3: Show information about logged-in users, sorted by their idle time
Code:
w -i
Motivation:
Sorting the output of the w
command by idle time allows administrators to identify users who have been inactive for a long time. This can be helpful in managing system resources or identifying idle sessions that can be terminated to free up resources.
Explanation:
The argument -i
is used with the w
command to sort the output based on the idle time of the logged-in users. This arranges the users in ascending order of their idle time, with the most idle user displayed first.
Example Output:
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
user3 pts/2 192.168.0.3 10:00 0.10s 0.01s 0.01s ls -l
user2 pts/1 192.168.0.2 09:30 1:45m 0.02s 0.02s vim myfile.txt
user1 pts/0 192.168.0.1 09:00 2:15m 0.05s 0.05s bash
In the example output above, the users are sorted based on their idle time. User3 has the least idle time, followed by user2, and user1 has the highest idle time.
Conclusion:
The w
command is a useful tool for displaying information about logged-in users and their activities. By using different arguments, such as -h
to remove the header or -i
to sort by idle time, you can customize the output according to your needs. This command provides valuable insights into user activity on the system and helps in managing resources effectively.