time – measure command execution time

time command
time command

time measures how long a command takes to execute, showing real, user, and system time.

Synopsis

time COMMAND [ARGS...]

Examples

Basic usage

$ time ls -la /etc

real    0m0.015s
user    0m0.004s
sys     0m0.008s

Time a script

$ time ./backup.sh

real    2m34.567s
user    0m12.345s
sys     0m5.678s

Time with output suppression

$ time find / -name "*.log" 2>/dev/null | wc -l

Understanding Output

FieldMeaning
realWall-clock time (total elapsed)
userCPU time in user mode
sysCPU time in kernel mode
  • real > user + sys: Process waited (I/O, sleep, network)
  • user + sys > real: Multiple CPUs used in parallel

Bash vs /usr/bin/time

The shell built-in and external command differ:

# Shell built-in
$ time ls

# External command (more detailed)
$ /usr/bin/time ls
$ /usr/bin/time -v ls    # Verbose (Linux)

Verbose output (GNU time)

$ /usr/bin/time -v ls
...
Maximum resident set size (kbytes): 2456
Minor (reclaiming a frame) page faults: 123
Voluntary context switches: 2
...

Common Patterns

Compare commands

$ time sort bigfile.txt > /dev/null
$ time sort -S 2G bigfile.txt > /dev/null

Benchmark script

for i in {1..10}; do
    time ./script.sh
done 2>&1 | grep real

Tips

  • Use /usr/bin/time for details: More info than bash built-in
  • Redirect output: time cmd 2>&1 to capture timing
  • Multiple runs: Average several runs for accuracy
  • I/O vs CPU: High real vs user+sys = I/O bound

See Also

  • date — Display time
  • watch — Run command repeatedly

Tutorials