Linux time Command Tutorial for Beginners (with Examples)
Sometimes, when you're executing a program, you might want to know its system resource usage. Like how much time the process spent in kernel mode and user mode, and other info.
Thankfully, there exists a tool - dubbed time - that's specifically built for this purpose. In this article, we will discuss the basics of the 'time' command using some easy to understand examples.
But before we do that, it's worth mentioning that all examples in this tutorial have been tested on an Ubuntu 18.04 LTS machine.
Linux time command
The time command in Linux allows you to run programs and summarize their system resource usage. Following is its syntax:
time [OPTIONS] COMMAND [ARGS]
Here's how the tool's man page describes it:
time run the program COMMAND with any given arguments ARG.... When COMMAND finishes, time displays
information about resources used by COMMAND (on the standard error output, by default).
If COMMAND exits with non-zero status, time displays a warning message and the exit status.
time determines which information to display about the resources used by the COMMAND from the
string FORMAT. If no format is specified on the command line, but the TIME environment variable
is set, its value is used as the format. Otherwise, a default format built into time is used.
Options to time must appear on the command line before COMMAND. Anything on the command line after
COMMAND is passed as arguments to COMMAND.
Following are some Q&A-styled examples that should give you a better idea on how the time command works.
Q1. How to use time command?
The basic usage is simple - just execute 'time' with the command/program you want to run as input.
For example, I used the time command in the following way:
time ping howtoforge.com
And here's the output:
PING howtoforge.com (126.96.36.199) 56(84) bytes of data.
64 bytes from 188.8.131.52 (184.108.40.206): icmp_seq=1 ttl=59 time=93.8 ms
64 bytes from 220.127.116.11 (18.104.22.168): icmp_seq=2 ttl=59 time=91.5 ms
64 bytes from 22.214.171.124 (126.96.36.199): icmp_seq=3 ttl=59 time=93.1 ms
64 bytes from 188.8.131.52 (184.108.40.206): icmp_seq=4 ttl=59 time=102 ms
--- howtoforge.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 91.510/95.249/102.490/4.267 ms
The last three lines in the output are added by the time command. While 'real' signifies the wall clock time the 'ping' command took from execution till termination, 'user' and 'sys' are the time taken by 'ping' the user space and kernel space, respectively. Detailed about these three times can be accessed here.
Q2. How to make 'time' write its output to a file?
If you want the time command to write its output to a file instead of the terminal, use the -o command line option, which expects a file name/path as input.
/usr/bin/time -o /home/himanshu/time-output.txt ping howtoforge.com
This command will display ping's output on stdout, while the 'time' command output will be written to the text file.
NOTE: We used /usr/bin/time instead of 'time' because the shell built-in time command doesn't offer the -o option.
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.
Q3. How to make time produce detailed output?
This can be done using the -v command line option. For example, when I used this option while running 'time' with a 'ping' command, following details were produced in the 'time' command output:
Command being timed: "ping howtoforge.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:11.77
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): 3064
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 158
Voluntary context switches: 14
Involuntary context switches: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
So you can see the time command produces a lot of other details in this mode.
Q4. How to customize time command output?
The time command also offers a 'format' command line option that lets you customize the output of this tool. It provides a set of resource specifiers that you can use to fetch any type of information supported by the 'time' command (see previous section).
For example, the time command in the following execution:
/usr/bin/time -f "\t%C [Command details],\t%K [Total memory usage],\t%k [Number of signals process received]" ping howtoforge.com
produced this output:
ping howtoforge.com [Command details], 0 [Total memory usage], 0 [Number of signals process received]
The time command man page contains details related to the format command line option.
The time command is mostly used by software developers and testers. However, there's no harm in knowing it even if you are not one of those, for you never know when you may require it. More info about this command can be accessed through the tool's man page.