Session and utilities

The time command

Measures how long a command or pipeline takes: real, user and system time.

Syntax

time <comando>Try it in the terminal →

Most useful options

-pBash reserved word: POSIX three-line format (real, user and sys). /usr/bin/time has the same option.
-v/usr/bin/time (GNU) only: very detailed report of memory, I/O and page faults. The reserved word does not have it.
-f formato/usr/bin/time (GNU) only: custom format, with %e for real time, %U for user time and %S for system time.
-o archivo/usr/bin/time (GNU) only: writes the result to a file instead of standard error.
-a/usr/bin/time (GNU) only: with -o, appends to the file instead of overwriting it.
-q/usr/bin/time (GNU) only: does not report signal terminations or nonzero exit codes.

Copy-ready examples

$ time echo holatimes a simple command. The timings go to standard error, after the normal output.

Example output

hola
$ time -p echo holauses the POSIX format, easy to read in a script.

Example output

hola
real 0.00
user 0.00
sys 0.00
$ time ls -l | wc -ltimes the whole pipeline: the time includes both ls and wc.
$ TIMEFORMAT='%3R'; time tar -czf copia.tgz proyectochanges the format with TIMEFORMAT and shows only the real time, with three decimals.
$ /usr/bin/time -v tar -czf copia.tgz proyectouses the GNU program, if it is installed, to see memory and I/O as well as time.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
bash: nosuchcmd: command not foundThe command does not exist or is misspelled. time still runs it, prints the timings and returns 127.Check the name with type before timing it.
bash: /usr/bin/time: No such file or directoryThe /usr/bin/time program is not installed. The time reserved word works the same.Use time without a path, or install the time program from your distribution if you need -v or -f.

Typical mistakes

  • Real time includes waits for disk, network or keyboard. user and sys are CPU work, so real can be larger than their sum.
  • time measures the whole pipeline, not only the first command: in time ls | wc -l both are measured.
  • TIMEFORMAT controls the format of the reserved word. If it is empty, bash prints no timing at all.

Differences from similar commands

  • /usr/bin/time: separate program with its own options (-v, -f, -o). It is not always installed.
  • date: shows the date. To measure a duration, store date +%s before and after and subtract.
  • sh: in /bin/sh on Debian and Ubuntu, dash has no time reserved word: the command fails unless /usr/bin/time exists.

Related commands

FAQ

What is the difference between real, user and sys?

real is wall-clock time from start to end. user and sys are CPU time in user mode and in system mode.

Is time a program or part of the shell?

In bash it is a reserved word, which is why it accepts pipelines. There is also /usr/bin/time, a separate program with more options.

Can I change the output format of time?

In bash, with the TIMEFORMAT variable. In /usr/bin/time, with the -f option.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course