Linux Commands

How to use time command in Linux

Time is the essential parameter to evaluate the efficiency or performance of any task. Such a good processor is judged by good processing speed which is evaluated on the basis of time. Similarly, in Linux, the “time” command is used to evaluate the processing time taken by different commands in their execution. There are different ways to do the same task, for example, we can edit the text files using the vim editor as well as the nano editor, with the help of the time command we can evaluate which editor takes less execution time, then we can use that editor for text editing purposes for better performance.

What is the use of time command in Linux

In Linux, the time command is used to determine the execution time taken by the processor to execute the specified command.

The general syntax of using the time command:

$ time [options] [command]

The explanation to the above syntax is:

  • Use the time clause to determine the time taken by the command
  • Choose any options along with the time clause
  • Type the command whose executed time you want to find out

For example, we execute a command which will take 4 seconds execution time and determine its time using the “time” command:

$ time sleep 4

The output displayed that the command was executed in 4.002 seconds, there are three types of values in the output real, user, and sys, the explanation to them is as:

  • Real: This is the actual time taken by the processor of the computer to execute the command from pressing the button to complete the command.
  • User: CPU time that is taken by the user mode.
  • SYS: This is the time taken by the system or the Kernel to execute the command.

We can also add an option by adding a flag of “-p”, that will display the time in portable POSIX format, to understand it, again run the above command using the “-p” flag:

$ time -p sleep 4

We will update the repository of the Linux and will monitor the time of update by using the time command:

$ time sudo apt update

It will take some time to update the repository:

It takes 36.289 seconds to update the packages repository. To understand more about the time command, we can run the help using the command:

$ help time

If we want that the output of the time command is not displayed on the screen, instead of that it should be saved in the text file we can run the following command:

$ /usr/bin/time -o output.txt sleep 4

Note: We will use the “/usr/bin/time” instead of the “time” command because the shell built-in time command does not support the “-o” option.

To view the output of the file “output.txt”, use the command:

$ cat output.txt

To have the detailed summary of the executed time, use the option “-v” with the “/usr/bin/time” command:

$ /usr/bin/time -v sleep 4

The above figure displays the detailed summary of the executed time for the command. In the last, Linux provides the manuals of all the built-in commands, to view the manual of the time command to seek some information, use:

$ man time


Conclusion

The time command is used in Linux for testing purposes; it is used to test the performance of newly created applications. In this write-up, we have discussed the use of the time command in Linux that is used to monitor the execution time of the commands in Linux. We have discussed two syntaxes, the built-in “time” command and “/usr/bin/time” command with its different options.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.