W Command in Linux

FREE Online Courses: Enroll Now, Thank us Later!

In this article, we will learn everything about the w command in Linux. First, we will look at what the w command is, why it is used, and understand the output of the w command as we take a closer look at the fields in the output, the syntax of the w command, and the options used along with it.

In the end, we will also look at some practical examples of the w command by pairing it with various options to understand its working. So pay attention, take notes, and read to the end for the best benefits.

What is Linux w command?

W is a command-line utility in Linux-based operating systems that allows administrators to view information about users currently logged in. This command is a sheer boon for System administrators, and they use this command quite frequently for  many purposes.

The w command prints out the username, where the users are logged in from, and what they are currently doing. We shall get a better picture of the output of the w command in one of the later sections of this article. 

The w command also gives various other information like how long the system has been running, the current time, the system load average, and a few others. 

Syntax of the w command in Linux

The simple syntax of the w command is shown below:

w <options> <username>

Let us look at the options used along with the w command.

<options>

This field takes in a range of options that specify how the w command functions, formats, and prints the output. You can specify multiple options in this field. 

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

<username>

This field takes in the user’s name you want to print information.

Options used with Linux w command

Unlike most of the commands in Linux, the w command has only a few associated options. Let us take a brief look at each one of them.

1. -h

This option does not print the headers. You can also write this option as “–no-header.”

2. -u

This option ignores the name of the user while figuring out the current process and CPU times. You can also write this option as “–no-current.”

3. -s

This option displays the abbreviated output. It also does not print the login time, PCPU or JCPU times. You can also write this option as “–short.”

4. -f

This option prints the “from” field. The default setting of the w command is not to print the “from” field. You can also write this option as “–short.”

5. -o

This option prints the output in the old style, meaning it will print blank space for idle times less than one minute. You can also write this option as “–old-style.”

6. –help

This option prints the help menu of the w command, as shown below:

help menu of the w command

7. -V

This option prints information about the current version of the w command.

version of the w command

Understanding the output of the w command

We get the following output if we run the w command by not pairing it with any other option.

output of the w command

The output shown above contains a lot of valuable information. So let us look at what each field is and what information it shows.

The output of the w command is split into two parts. The first line consists of the following fields:

  • System time: The current system time.
  • Uptime: The time frame the system has been active.
  • The number of users: displays the number of users currently logged in.
  • Average system load: prints the average number of jobs running on the system.

The second line in the output of the w command contains the field that are shown below:

  • USER: The names of currently logged-in users.
  • TTY: displays the name of the terminal the user is logging in from.
  • FROM: The name or IP address of the terminal.
  • LOGIN@: The time the user logged in.
  • IDLE: The time since the user last used the terminal.
  • JCPU: The total run time of all system processes that are attached to the terminal.
  • PCPU: Elapsed time for the user’s current process.
  • WHAT: The name of the user’s current process.

Now that we have covered the basics and the theoretical part of the w command let us understand how the w command and its options work by looking at some practical examples of it in the terminal of Ubuntu 20.04.

Displaying in short format

As we saw, the output of the w command is exceptionally long and consists of many fields. Nonetheless, you can direct the w command to print the output in a short format – print only the USER, TTY, FROM, IDLE, and WHAT fields by using the option “-s” as shown:

w -s

displaying in short format

Printing output without the headers

If you want your output to be clean and simple, and have no headers, pair the w command with the option “-h” as shown:

w -h

printing output without the headers

Ignoring a username

If you want to ignore a specific username, meaning you don’t want to print information about that user, use the option “-u” followed by the username as shown:

w -u <username>

ignoring a username

Displaying the IP address

If you want to display another field that shows the name of the terminal or the name of the remote host the user is logged in from, combine the w command with the option “-i” as shown:

w -i

displaying the ip address

Displaying the output in the old style

If you display the output in the old style leaves a blank space under the PCPU IDLE, and, JCPU fields for users that have been idle for less than one minute. To make the w command print in the old style, use the option “-o” as shown:

w -o

displaying the output in the old style

It is not showing the “from” field.

By default, the w command prints the “from” fields. Nonetheless, if you don’t want it in the output, you can toggle it off by pairing the w command with the option “-f” as shown:

w -f

 

not showing the from field

Displaying user output

If you run the w command followed by a username, the w command displays the information only for that specific user, as shown below:

displaying user output

Summary

As you have seen, the w command displays information about users currently logged in. You have now understood the w command as we discussed what it is used for, the syntax of the w command, the options used along with it, and the w command’s output. We have also seen many practical examples of the w command by pairing the available options. 

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *