Fundamentals 8 min read

Master Linux ‘time’: Measure Script Execution Like a Pro

This guide explains how to use the Linux time command to accurately measure script or command execution duration, covering basic usage, output redirection, detailed statistics, and custom formatting for precise performance testing in time‑critical environments.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Linux ‘time’: Measure Script Execution Like a Pro

Programmers often need to measure how long a script or command runs, especially when dealing with time‑sensitive systems such as NTP synchronization.

The time command provides a simple way to test execution time and obtain real, user, and system durations.

1. Basic usage of time

The most straightforward form is:

$ time ping baidu.com
... (ping output) ...
real    0m11.173s
user    0m0.004s
sys     0m0.002s

In the result, real shows the total elapsed wall‑clock time, while user and sys represent the time spent in user space and kernel space respectively.

2. Writing time information to a file

Use the -o option to direct the timing report to a file:

$ /usr/bin/time -o /home/alvin/time-output.txt ping baidu.com

The command’s normal output still appears on the screen, but the timing data is saved to time-output.txt. To append instead of overwrite, use -a:

$ /usr/bin/time -a /home/smart/time-output.txt ping linoxide.com

3. Displaying more detailed timing information

Adding the -v flag produces a verbose report with extensive statistics:

$ /usr/bin/time -v ping baidu.com
... (detailed statistics including CPU usage, memory, page faults, context switches, etc.) ...

4. Customizing the output format

The default output shows only real, user, and sys. You can define a custom format with -f and a list of format specifiers (e.g., %E for elapsed time, %I for inputs, %O for outputs):

$ /usr/bin/time -f "Elapsed Time = %E, Inputs %I, Outputs %O" ping baidu.com

To insert line breaks, include \n in the format string:

$ /usr/bin/time -f "Elapsed Time = %E 
 Inputs %I 
 Outputs %O" ping baidu.com

This produces output such as:

Elapsed Time = 0:03.92
Inputs 0
Outputs 0

By leveraging these options, you can tailor the time command to provide exactly the performance data you need for testing and optimization.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performance measurementShell scriptingtime command
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.