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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-time-command-in-Linux-1.png" data-lazy- height="166" src="data:image/svg xml,” width=”419″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-time-command-in-Linux-2.png" data-lazy- height="135" src="data:image/svg xml,” width=”639″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-time-command-in-Linux-3.png" data-lazy- height="160" src="data:image/svg xml,” width=”975″>

It will take some time to update the repository:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-time-command-in-Linux-4.png" data-lazy- height="294" src="data:image/svg xml,” width=”899″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-time-command-in-Linux-5.png" data-lazy- height="394" src="data:image/svg xml,” width=”924″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-time-command-in-Linux-6.png" data-lazy- height="59" src="data:image/svg xml,” width=”700″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-time-command-in-Linux-7.png" data-lazy- height="108" src="data:image/svg xml,” width=”943″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-time-command-in-Linux-8.png" data-lazy- height="500" src="data:image/svg xml,” width=”904″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-time-command-in-Linux-88.png" data-lazy- height="42" src="data:image/svg xml,” width=”921″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-use-time-command-in-Linux-9.png" data-lazy- height="596" src="data:image/svg xml,” width=”1286″>

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

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/hammad–150×150.jpg61c81f0d38fa7.jpg" height="112" src="data:image/svg xml,” width=”112″>

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.