The time command might not be the most frequently used one in a Linux terminal, but that doesn’t diminish its usefulness. If you want to know the time required for the command to run, as well as some other details, this is the right command for you.

The developers are among those that commonly use this command because they often need more information about the script or program’s performance. Let’s take a look at how you can take advantage of the Linux time command.

Basic Syntax

When it comes to the time utility, the basic syntax is simple and looks like this:

time

That’s everything you need to write in your terminal before you hit enter and Linux displays the results on the screen. Keep in mind that the system will show three different results:

  • Elapsed (total, real) – this shows the actual time from the moment that you press enter to execute the command until it is completed.
  • User – the portion of CPU time utilized in the user mode.
  • Sys (system) – the portion of CPU time utilized in the kernel mode.

Ping the Time of a Website

There are various things you can do with the time command, and one of them is to ping your or any other website. To do that, you should use the following line:

time ping mywebsite.com

ping the time of a website

Write the “Time” Results to a File

You can also tell Linux to write the results of the “time” command to a certain file. Here is the line that you should use:

/usr/bin/time -o /www/var/time.txt ping mywebsite.com

The crucial thing here is the change at the beginning of the line. We used a different way of typing the command because the “-o” function is not supported by the integrated time command.

The “-o” option is what tells the system to write the results to a file. The path we specified is the destination of the desired folder. After executing the above line, you can see the results of the “time” command (in this case, ping of the “mywebsite.com” site) in the “time.txt” file in your “/www/var/” folder.

Change the Time Command Results

We already mentioned that you would get three lines as a result of a time command by default. But what happens when you want to adjust the results and need additional information?

Here are some of the options you can use:

  • E – use format of a clock to show elapsed time
  • F – page fault number
  • I – the process’ system input number
  • P – CPU percentage of the received job
  • S – the total amount of seconds of the CPU used in the kernel mode by the system
  • U – the total amount of seconds of the CPU used in the user mode
  • Z – the page size of the system shown in KB
  • c – how many times the process was context-switched
  • e – use seconds to show the elapsed time
  • k – it shows how many signals did the process receive
  • r – how many socket messages were sent to the process
  • s – it shows how many socket messages did the process send
  • x – the command’s exit status

Keep in mind that there is a difference in using lowercase and uppercase letters.

Here is an example of adjusting the output of the time command to your needs:

time -f "Total time %E, Inputs %I, Outputs %O"

In this case, the result can look like this:

Total time =1:14:22, Inputs 11, Outputs 7

Feel free to tailor the results to your needs based on the above suggestions. You can also use the “man time” to get an overview of everything that you can do with the time command.

Conclusion

Linux time command is a useful utility that can be helpful to developers and those who want to know more about their system, scripts, and programs. The crucial thing to keep in mind is that Ubuntu might not accept the default “time” command, which is why you should use “usr/bin/time” to run the command.