In some cases, when working with the Linux terminal, you need to know the execution time of certain commands, for example, to track down problems. There is a time utility for this task. Specifically, it will be discussed in our article.
We will explain how the Linux time command works and what its syntax is. Then let’s move on to the description of the available options. And at the end we will mention some popular use cases.
Syntax and options
The utility runs a user-specified command and then displays information about the time of its execution. It has a fairly convenient syntax. First you need to specify options for time, then – the command to be executed, and at the end – the arguments to it:
$ time options command_to_execute arguments
Consider the list of available options:
- -o , –output – save data to the selected file instead of standard output in the terminal. This will overwrite the old data in the file.
- -a , –append Append new information to the file rather than overwriting the old one. The option is only useful in combination with -o .
- -f , –format – select a specific output format. Details about formatting are described in the next section of the article.
- -p , –profitably Use the output format to conform to the POSIX 1003.2 standard.
- -v , –verbose Print detailed information about program execution.
- -V , –version Display the version of the time utility.
This list contains only the main options. You can get detailed information with the command:
man time
Output formatting options
By default, time can display information in a form that is not comfortable to read.
For this reason, it is desirable for it to set output formatting options, which will be discussed now. There are three of them in total. Let’s take the apt-get update command as an example to consider them.
The -v option is used to display verbose information:
sudo time -v apt-get update
The -p option is needed to output data in a format conforming to the POSIX 1003.2 standard:
sudo time -p apt-get update
And with the help of the -f or –format option, the formatting of the output is specified in detail. This point is worth considering in more detail.
The format string typically includes resource specifiers and plain text. The % sign means that the character following it should be taken as a resource specifier.
The sign specifies the separator character. There are three options available: t – tab, n – new line, \ – backslash. If you specify any other character after, then a question mark ( ? ) will appear in the terminal , which indicates an input error.
The rest of the text in the format string is copied in its entirety to the output field. At the same time, time always starts the output from a new line after the information about the execution of the command itself.
Consider the available resource specifiers:
- % – literal %. That is, to display the percent sign, you need to specify %% in the command .
- C is the name of the command and the arguments used.
- D is the average size of the unpartitioned data area. Displayed in kilobytes.
- E – real time of command execution in the usual hourly format. Displayed as [hours:]minutes:seconds .
- N is the number of major or I/O related errors that occurred during the execution of the process.
- I is the number of file system entries.
- K is the average value of memory used for code (text), initialized data (data) and stack (stack). Displayed in kilobytes.
- M is the maximum size of the resident set during the execution of the process in kilobytes.
- O is the number of exits from the file system.
- P is the percentage of CPU (central processing unit) load.
- R is the number of minor errors.
- S is the time in seconds during which the CPU was used by the system on behalf of the process in supervisor mode (kernel mode).
- U is the time in seconds during which the CPU was used by the process directly in user mode.
- W is the number of times the process has been swapped out of RAM.
- X – I do not understand here .
- Z is the size of the system page. This value is a constant, but it differs between systems.
- c is the number of involuntary context switches during process execution.
- e – real time of command execution in the usual hourly format. Displayed in seconds.
- k is the number of signals that reached the process.
- p is the average size of the process’ unshared stack, in kilobytes.
- r is the number of received socket messages.
- s is the number of socket messages sent.
- t is the average size of the process’s resident set, in kilobytes.
- w is the number of voluntary context switches during the execution of the process.
- x is the return code for the command.
These were all resource specifiers used when choosing the format for time. Now let’s move on to the scenarios for using the command.
How to check the execution time of a Linux command
We will look at three main examples that are used quite often: output to the terminal, output to a separate file, and output with formatting.
1. Output runtime to terminal
As an example, we will take the sleep command, which pauses for a specified amount of time. This will be a very illustrative example, because the pause time will coincide with the execution time of the command in time:
time sleep 3
Please note that the time is written immediately in three columns. Let’s go through each of them:
- real is the total time from the beginning of the process execution to its completion.
- user is the amount of time the process has been active in user mode.
- sys is the time during which the process was active in supervisor mode (kernel mode).
The next useful script is to display the time it takes to load the title of a web page through the curl utility. Let’s take our site as an example:
time curl -I https://linuxinf.com
I would also like to mention the situation when executing a command switches the terminal window to a different mode, for example, when you launch the nano editor to change the /home/root-user/script.txt file:
time nano /home/root-user/scripts/main_script.txt
After the editor is finished, you will see the total time you worked on the file.
Now you know how to view the execution time of a Linux command.
2. Output to a file
Information about the results of the time command can be saved to a separate file using the -o option . In this case, they will not be displayed in the terminal window. Take, for example, extracting the contents of the ~/data/data.tar.gz archive using the tar utility. Save the runtime information in a new file ~/data/data_time.txt. Let’s also add the -v option to the command to get more details:
sudo time -v -o ~/data/data_time.txt tar -xvf ~/data/data.tar.gz -C ~/data
The file will have the following content.
When using the -o option , be aware that it overwrites the old information in the output file with the new one. This is applicable when creating new files, but not suitable for logging. With the additional option -a , overwriting content is replaced by adding new information:
sudo time -v -a -o ~/data/data_time.txt tar -xvf ~/data/data.tar.gz -C ~/data
3. Output with formatting
The original data formatting option is not suitable in all cases. As an example, we will take the previously used command to extract the archive, but we will not save the data to a file. In doing so, we are interested in:
- Information about the command itself and the given options is the %C specifier .
- CPU utilization – specifier %P .
- The total execution time in seconds is the %e specifier .
For comfortable perception, each item will be displayed on a separate line using the separator n . In doing so, they will all be signed in an understandable way.
Here is what the final version of the command looks like:
sudo time -f "Command Information:n%CnCPU utilization: %PnLead time: %e seconds" tar -xvf ~/data/data.tar.gz -C ~/data
Note that all text, except for the characters after the % and , is fully displayed in the terminal. This is a convenient markup.
Findings
In this article, you learned how the Linux time command works, which monitors the execution of a given command and gives detailed information about the execution time, etc. The main difficulty in using it is understanding the options for output formatting. But if you understand them, then no more problems will arise. For your convenience, we have mentioned specific use cases.