Linux time Command: Measure Command Execution Time
time measures the elapsed time and CPU usage time for a command or pipeline. The time keyword in Bash can measure shell built-in commands, functions, and pipelines, while the external GNU /usr/bin/time can provide detailed resource usage such as memory and I/O.
For simple speed checks, Bash time is convenient, and if you need operating system-level statistics like maximum memory and page faults, it is better to explicitly run the external time.
What is the time command?
In Bash, time is a keyword recognized as syntax before ordinary executable files. Because it is a keyword, it can naturally measure not only a single external program but also entire pipelines, shell functions, and built-in commands.
Many Linux systems also have the GNU Time executable installed separately. Although it has the same name, the options and output information are different, so check your current environment with the following command.
type -a time command -V time ls -l /usr/bin/time
Bash time basic syntax
time [-p] command [arguments...] time [-p] command1 | command2
Place time before the command you want to measure.
time sleep 1 time find /usr -type f >/dev/null
When the command finishes, usually three values real, user, and sys are displayed.
The meaning of real, user, sys
| Item | Meaning |
|---|---|
real |
The actual elapsed time from start to finish. It also includes the time spent waiting due to disk, network delays, and other processes. |
user |
The cumulative time the process has used the CPU in user mode. |
sys |
The cumulative CPU time used in kernel mode for the process. |
If multiple cores or processes are used in parallel, user + sys can be greater than real. Conversely, if I/O wait is long, real can be large while CPU time is small.
Measuring the entire pipeline
The Bash reserved word time measures until the entire following pipeline is finished.
time find /var/log -type f -print0 | xargs -0 wc -l >/dev/null
If you want to measure only the first command of a pipeline separately, you need to clearly organize groups or be careful with the measurement scope when using external tools. It is important to first determine the start and end points of the task you want to measure.
Measuring Multiple Commands as a Single Task
By grouping a list of commands with braces or a subshell, you can measure the total time.
time {
make clean
make -j4
}
time ( cd /srv/app ./build.sh )
A brace group runs in the current shell, while a parenthesis group runs in a subshell. Take this difference into account if directories or variables change during the task.
Portable Output Format -p
time -p sleep 1
-p uses POSIX-style real, user, sys output. It is useful when you need to simply parse results on multiple systems, but automated requirements including locale and precision need to be considered separately.
Changing Bash Output with TIMEFORMAT
The output of the Bash keyword can be adjusted using the TIMEFORMAT variable.
TIMEFORMAT='elapsed=%3R s user=%3U s sys=%3S s' time sleep 1
| Format | Meaning |
|---|---|
%R |
Elapsed Time |
%U |
User-mode CPU time |
%S |
System-mode CPU time |
%P |
Display CPU usage as a percentage |
%% |
Literal percent sign |
TIMEFORMAT affects the current shell. To apply it temporarily, preserve the existing value or set it within a subshell.
( TIMEFORMAT='elapsed=%R s' time ./job.sh )
Use external GNU time
Specify the full path to reliably use options of external commands.
/usr/bin/time ./job.sh /usr/bin/time -v ./job.sh
-v outputs detailed information provided by the system, such as maximum resident memory, page faults, file system I/O, and context switches. The actual items and values provided may vary depending on the operating system.
Frequently used options of GNU time
| Option | Description |
|---|---|
-p |
Prints time in POSIX format. |
-v |
Prints detailed resource usage statistics. |
-f FORMAT |
Formats the output using format specifiers. |
-o FILE |
Writes the measurement results to the specified file. |
-a |
-a Appends to the file without overwriting it. |
Output only the time and memory in the desired format
/usr/bin/time -f 'elapsed=%e max_rss=%MKB' ./job.sh
%e represents elapsed time, and %M represents the maximum resident set size. Check the units and availability based on the GNU Time manual and your current system.
Save measurement results to a file
/usr/bin/time -v -o time-report.txt ./job.sh /usr/bin/time -v -a -o time-report.txt ./job.sh
Using -o allows you to separate the standard error of the command being measured from the time report, which is convenient for automation.
Why time output goes to standard error
Time information is output to standard error by default. This is to prevent the time results from mixing with the command's standard output when it is piped or redirected to a file.
time ./job.sh >job-output.txt
This command saves only the standard output of the job to a file and displays the time information on the terminal. To redirect only the time information of a Bash builtin separately, you need to handle the standard error of the entire group, but doing so may also include the job's own standard error.
{ time ./job.sh >job-output.txt; } 2>time-and-errors.txt
For separating only the time report, using the external GNU Time -o is clearer.
Points to note for accurate comparison
- The first execution and subsequent executions may differ due to file system caching, JIT compilation, and connection reuse.
- Control background load, CPU frequency scaling, and virtual machine resource contention.
- Very short commands may have results significantly affected by the measurement overhead itself.
- Repeat multiple times and compare the median or the distribution.
- Execution time may vary depending on whether the output target is a terminal or a file.
- Also check whether the speed difference actually changes the task result or error rate.
Comparison of Bash time and /usr/bin/time
| Category | Bash time |
GNU /usr/bin/time |
|---|---|---|
| Type | Bash Keywords | External Executable Files |
| Pipeline | It is easy to measure the entire pipeline directly. | By default, it runs the specified program. |
| Shell Functions and Built-in Commands | Can be measured directly. | It is difficult to execute directly without going through a separate shell. |
| Detailed Resource Information | It is centered on basic time statistics. | Using -v provides statistics such as memory and I/O. |
| Output Customization | TIMEFORMAT |
-f, -o, -a |
Frequently Asked Questions
Can the user time be greater than the real time?
Yes. If CPU time used across multiple processes or CPU cores accumulates, the user CPU time can be greater than the actual elapsed time.
time -v does not work.
This could be because the Bash reserved word is interpreted first. If GNU Time is installed, use the full path like /usr/bin/time -v command.
Does time change the exit status of a command?
It generally returns the exit status of the command or pipeline being measured. In a pipeline, the result may vary depending on Bash's pipefail setting.









