Mastering Linux ‘time’: Measure, Log, and Customize Command Execution
This guide explains how to use the Linux time command to benchmark scripts, capture real, user, and system durations, redirect results to files, obtain detailed statistics with the -v option, and create custom output formats using format specifiers and escape sequences.
1. Basic usage of time
The simplest form is time <command>. For example:
$ time ping baidu.com
PING baidu.com (123.125.114.144) 56(84) bytes of data.
64 bytes from 123.125.114.144: icmp_seq=1 ttl=56 time=2.83 ms
...
--- baidu.com ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 10818ms
rtt min/avg/max/mdev = 2.765/2.808/2.862/0.039 ms
real 0m11.173s
user 0m0.004s
sys 0m0.002sIn the result, real is the wall‑clock time from start to termination, while user and sys show the CPU time spent in user and kernel space respectively.
2. Writing timing information to a file
Use the -o option to direct the timing summary to a file while the command’s normal output still appears on the terminal:
$ /usr/bin/time -o /home/alvin/time-output.txt ping baidu.comIf you want to append instead of overwriting, use -a:
$ /usr/bin/time -a /home/smart/time-output.txt ping linoxide.com3. Getting more detailed statistics
Adding the -v flag expands the report with many additional fields such as page faults, context switches, memory usage, and I/O statistics.
$ /usr/bin/time -v ping baidu.com
...
Command being timed: "ping baidu.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:03.64
Maximum resident set size (kbytes): 2140
... (other detailed metrics)4. Customizing the output format
The -f option lets you specify a format string composed of format specifiers. Some common specifiers are: %E – elapsed (wall‑clock) time %I – number of file system inputs %O – number of file system outputs %P – CPU usage percentage %M – maximum resident set size (KB) %x – exit status
Example of a custom format that prints elapsed time, inputs, and outputs:
$ /usr/bin/time -f "Elapsed Time = %E, Inputs %I, Outputs %O" ping baidu.comTo insert line breaks, include \n in the format string:
$ /usr/bin/time -f "Elapsed Time = %E
Inputs %I
Outputs %O" ping baidu.comResult:
Elapsed Time = 0:03.92
Inputs 0
Outputs 05. Summary of useful options
time <command>– basic timing -o <file> – write summary to file (overwrite) -a <file> – append summary to file -v – verbose, detailed statistics -f <format> – custom output format using specifiers
These options enable precise performance measurement for scripts and commands, especially in environments where timing accuracy is critical.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
