How to use Linux time command

Photo of author

By rasho

The time command is used to determine how long a given command takes to run. It is useful for testing the performance of your scripts and commands.
Good thing is time command comes preinstalled in most Linux distributions, so you don’t have to bother with installation.
In this article, we will discuss the basics of the time command using some easy to understand examples.

Using Linux Time Command

To use time command, just execute time with the command/program you want to run as input. Please check below example:

time wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.0.6.tar.gz

What will be printed as an output depends on the version of the time command you’re using:

$ time wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.0.6.tar.gz
--2019-04-05 11:59:36--  https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.0.6.tar.gz
Resolving cdn.kernel.org (cdn.kernel.org)... 151.101.13.176, 2a04:4e42:3::432
Connecting to cdn.kernel.org (cdn.kernel.org)|151.101.13.176|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 162621355 (155M) [application/x-gzip]
Saving to: ‘linux-5.0.6.tar.gz’

linux-5.0.6.tar.gz  100%[===================>] 155,09M  2,77MB/s    in 66s

2019-04-05 12:00:52 (2,34 MB/s) - ‘linux-5.0.6.tar.gz’ saved [162621355/162621355]


real	1m15,289s
user	0m0,708s
sys	0m1,787s
  • real or total or elapsed (wall clock time) is the time from start to finish of the call. It is the time from the moment you hit the Enter key until the moment the wget command is completed.
  • user – amount of CPU time spent in user mode.
  • system or sys – amount of CPU time spent in kernel mode.

[ads]

How to Make time command writes its output to a file

To write the time command output to a file instead of the print out screen, use the -o command line option, which expects a file name/path as input.

$ /usr/bin/time -o ~/time-output.txt wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.0.6.tar.gz

Now we will display wget output on stdout, while the time command output will be written to the text file.
[box type=”note” align=”” class=”” width=””]Note: We used /usr/bin/time instead of time because the shell built-in time command doesn’t offer the -o option. [/box]
By default, everytime you run this command, the output file will be overwritten. However, if you want, you can make sure new output is appended by using the -a command line option.

How to make time produce detailed output

We can use the -v command line option to produce detailed output.

/usr/bin/time -v ping lintut.com

Example output:

.....
	Command being timed: "ping lintut.com"
	User time (seconds): 0.00
	System time (seconds): 0.00
	Percent of CPU this job got: 0%
	Elapsed (wall clock) time (h:mm:ss or m:ss): 0:05.74
	Average shared text size (kbytes): 0
	Average unshared data size (kbytes): 0
	Average stack size (kbytes): 0
	Average total size (kbytes): 0
	Maximum resident set size (kbytes): 3244
	Average resident set size (kbytes): 0
	Major (requiring I/O) page faults: 1
	Minor (reclaiming a frame) page faults: 165
	Voluntary context switches: 18
	Involuntary context switches: 0
	Swaps: 0
	File system inputs: 128
	File system outputs: 0
	Socket messages sent: 0
	Socket messages received: 0
	Signals delivered: 0
	Page size (bytes): 4096
	Exit status: 0

How to customizing time command output

There are a large number of formatting options as shown in the following list:

C – Name and command line arguments used
D – Average size of the process’s unshared data area in kilobytes
E – Elapsed time in a clock format
F – Number of page faults
I – Number of file system inputs by the process
K – Average total memory use of the process in kilobytes
M – Maximum resident set the size of the process during the lifetime in Kilobytes
O – Number of file system outputs by the process
P – Percentage of CPU that the job received
R – Number of minor or recoverable page faults
S – Total number of CPU seconds used by the system in kernel mode
U – Total number of CPU seconds used by user mode
W – Number of times the process was swapped out of main memory
X – Average amount of shared text in the process
Z – System’s page size in kilobytes
c – Number of times the process was context-switched
e – Elapsed real time used by the process in seconds
k – Number of signals delivered to the process
p – Average unshared stack size of the process in kilobytes
r – Number of socket messages received by the process
s – Number of socket messages sent by the process
t – Average resident set size of the process in kilobytes
w – Number of time the process was context-switched voluntarily
x – Exit status of the command

We can use the formatting switches as follows:

/usr/bin/time -f "\t%C [Command details],\t%K [Total memory usage],\t%k [Number of signals process received]" ping lintut.com

produced this output:

ping lintut.com [Command details],	0 [Total memory usage],	0 [Number of signals process received]

[ads]

Linux Time Command Versions

There are three-time command Versions, Bash, Zsh and Gnu time command. We can use the type command to determine whether time is a binary or a built-in keyword.

type time

Example output:

# Bash
  time is a shell keyword

  # Zsh
  time is a reserved word

  # GNU time (sh)
  time is /usr/bin/time

The time command man page contains details related to the format command line option.

Conclusion

The time command is mostly used by software developers and testers. More info about this command can be accessed through the tool’s man page. In case if you find this article informative then please do share your feedback and comments.

Leave a Comment