Linux Time Command

By 

Updated on

4 min read

Linux Time Command

When optimizing scripts or comparing two approaches to the same task, knowing exactly how long each one takes is the first step. The time command measures execution time and breaks the result into wall clock time, user CPU time, and kernel time.

For example, if you have two different scripts doing the same job and you want to know which one performs better, you can use the time command to determine the duration of execution of each script.

Time Command Versions

Both Bash and Zsh, the most widely used Linux shells, have their own built-in versions of the time command which take precedence over the GNU time command.

You can use the type command to determine whether time is a binary or a built-in keyword.

Terminal
type time
output
# Bash
time is a shell keyword

# Zsh
time is a reserved word

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

To use the GNU time command, specify the full path to the binary, usually /usr/bin/time, or use the env command. In Bash, a leading backslash (\time) also bypasses the shell keyword and runs the external command found in PATH.

GNU time lets you format the output and reports additional resource statistics such as memory use, file-system I/O, and context switches.

Using Linux Time Command

In the following example, we are going to measure the time taken to download the Linux kernel using the wget tool :

Terminal
time wget https://cdn.kernel.org/pub/linux/kernel/v7.x/linux-7.0.12.tar.xz

The output depends on the time implementation you are using:

output
# Bash
real	0m33.961s
user	0m0.340s
sys	0m0.940s

# Zsh
0.34s user 0.94s system 4% cpu 33.961 total

# GNU time (sh)
0.34user 0.94system 0:33.96elapsed 4%CPU (0avgtext+0avgdata 6060maxresident)k
0inputs+201456outputs (0major+315minor)pagefaults 0swaps
  • 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, and is usually the most useful number.
  • user - amount of CPU time spent in user mode.
  • system or sys - amount of CPU time spent in kernel mode.

Using Bash time

Bash implements time as a reserved keyword, which lets it time shell constructs such as pipelines. To display output in the portable POSIX format, use -p:

Terminal
time -p sleep 1
output
real 1.00
user 0.00
sys 0.00

To time an entire pipeline in Bash, place time before the first command:

Terminal
time wget -qO- https://example.com | wc -c

Bash waits for the complete pipeline and reports the combined resource use. This differs from the external GNU command, which can directly execute only one command and its arguments.

Bash formats its timing summary with the TIMEFORMAT variable. For example, the following format prints elapsed time and CPU usage:

Terminal
TIMEFORMAT='Elapsed: %3R sec  CPU: %P'
time sleep 1
output
Elapsed: 1.002 sec  CPU: 0.00

The Bash format codes differ from GNU time specifiers. %R is elapsed time, %U is user CPU time, %S is system CPU time, and %P is the CPU percentage.

GNU time Options

The GNU time binary supports a -f (or --format) flag that lets you customize the output using %-format specifiers.

GNU time writes its statistics to standard error by default. The -o option sends them to a separate file, which is useful when the measured command also writes progress or errors to standard error.

For example, use wget -q to suppress download progress and print only elapsed time and peak memory usage:

Terminal
/usr/bin/time -f "Elapsed: %e sec  Max memory: %M KB" \
  wget -q https://cdn.kernel.org/pub/linux/kernel/v7.x/linux-7.0.12.tar.xz
output
Elapsed: 33.96 sec  Max memory: 6060 KB

Common format specifiers:

  • %e - elapsed real time in seconds
  • %U - user CPU time in seconds
  • %S - system CPU time in seconds
  • %M - maximum resident set size in KB
  • %P - CPU percentage used by the process

To see a complete breakdown of every resource the process used, pass -v (verbose):

Terminal
/usr/bin/time -v wget -q https://cdn.kernel.org/pub/linux/kernel/v7.x/linux-7.0.12.tar.xz
output
	Command being timed: "wget -q https://cdn.kernel.org/pub/linux/kernel/v7.x/linux-7.0.12.tar.xz"
	User time (seconds): 0.34
	System time (seconds): 0.94
	Percent of CPU this job got: 4%
	Elapsed (wall clock) time (h:mm:ss or m:ss): 0:33.96
	Maximum resident set size (kbytes): 6060
	Minor (reclaiming a frame) page faults: 315
	Voluntary context switches: 335
	Involuntary context switches: 68
	Swaps: 0
	File system outputs: 201456
	Exit status: 0

The -v flag is only available with the GNU time binary. Using it with the Bash keyword returns an invalid option error.

To measure a full pipeline with GNU time, run the pipeline through a shell:

Terminal
/usr/bin/time -f "Elapsed: %e sec" \
  sh -c 'wget -qO- https://example.com | wc -c'

The shell is the command GNU time executes, so the measurement includes every process in the pipeline.

Conclusion

The Bash time keyword covers normal commands and pipelines, with TIMEFORMAT controlling its output. Use /usr/bin/time when you need memory statistics, detailed resource usage, or output written to a separate file. For the complete list of GNU format specifiers, see the time man page .

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page