How to Use the time Command in Linux

How to Use the time Command in Linux

A command can finish without showing how long it took. The Linux time command reports a command’s elapsed duration and CPU time.

I’ll show how to time a command and choose the right time implementation for its summary.

TL;DR: Time a Linux Command

Prefix a command with time to measure it, and use /usr/bin/time when you need GNU options such as a custom format or verbose resource data.

  • Use type time to identify what your shell runs.
  • Read elapsed as wall-clock duration, user as CPU time in user mode, and sys as CPU time in the kernel.
  • Use Bash’s keyword for pipelines and shell functions, or GNU time for flags such as -f and -o.

What Does the time Command Measure?

The time command reports elapsed duration and CPU time for a command. I checked Bash’s resolution with type time and it selected the shell keyword, so identify the implementation before choosing options.

  • Elapsed time includes waiting until the command exits.
  • User time counts CPU work in the program.
  • System time counts CPU work the kernel performs for it.

For a fair comparison, use identical input and machine conditions, then repeat each command. System load and filesystem cache can change a short run, so compare repeated measurements rather than treating one duration as a benchmark.

How to Use the time Command in Linux

To measure a command, prefix its name and arguments with time. Choose the syntax based on which implementation the shell resolves.

Step 1: Identify the time implementation

Save this command in a file named identify_time.sh to see how Bash resolves it:

type time

Run the saved file with Bash:

bash identify_time.sh

I ran the saved file with Bash and got:

time is a shell keyword
Bash reports time as a shell keyword
Bash resolves time as a shell keyword.

Step 2: Measure a command

The timing keyword goes before a command and its arguments. A local script keeps this example focused on timing rather than network activity.

/usr/bin/time -f 'Elapsed: %e sec' bash timing_demo.sh

The captured output was:

step 1
step 2
step 3
Elapsed: 0.00 sec
GNU time summary after running a short shell script
The elapsed-time summary appears after the script output.
/usr/bin/time -v command
/usr/bin/time -p command

Step 3: Choose a format or save the summary

GNU time’s format option lets you choose which measurements appear. I use a custom format when a report needs elapsed seconds, CPU seconds, and peak resident memory together.

  • %e reports elapsed seconds.
  • %U reports user CPU seconds.
  • %S reports system CPU seconds.
  • %M reports peak resident memory in kilobytes.
/usr/bin/time -f 'real=%e user=%U sys=%S max-kb=%M' command

Add the measured program and its arguments after the format. Choose an output file when the timing summary should stay separate from terminal output.

/usr/bin/time -o timing.log command

GNU time overwrites an existing output file rather than appending to it, so choose a fresh filename if its earlier contents matter.

Which time Command and Output Should You Use?

Choose the Bash keyword for shell constructs and GNU time for its additional report options. I use the format option when I need named measurements rather than the default summary.

SituationUseReason
One external programtime commandQuick elapsed and CPU summary
Bash pipeline or functionBash time keywordThe shell can measure its own constructs
GNU format, verbose stats, or log file/usr/bin/timeSupports GNU options where installed

GNU time writes its summary to standard error by default, separate from the measured program’s standard output. Send the streams to different destinations when a script consumes the program output.

Bash’s keyword can time pipelines and grouped commands, while GNU time accepts one command with its arguments. Wrap a pipeline in a shell when using GNU time, as in /usr/bin/time sh -c ‘grep needle file | wc -l’.

Use nohup to keep a command running after a terminal closes, or check overall load with uptime.

Common errors when timing Linux commands

Timing problems can come from selecting a shell keyword when GNU options are expected, redirecting the wrong stream, or measuring too briefly. Check which implementation runs, then match the fix to the result.

  • Option rejected: the shell selected its keyword. Use the explicit GNU executable with -f.
  • Timing absent from a file: GNU time writes the summary to standard error by default. Use -o timing.log to save it.
  • Elapsed value prints zero: the run is shorter than the displayed precision. Measure a longer representative workload and repeat it under similar conditions.

Conclusion: Read the Right Timing Value

When you inspect a timing summary, match each value to the question you are asking: elapsed duration for end-to-end delay, CPU values for processor work. GNU and Bash manuals document the exact formats for their respective implementations.

For exact option behavior, consult the GNU time manual and the Bash pipeline manual. To read a command’s own documentation, see our guide to the Linux man command.

FAQ

These answers cover common questions about command timing.

How do I check command execution time in Linux?

Prefix a command with time. Use /usr/bin/time for GNU options or custom formatting.

Why does time show real, user, and sys values?

Elapsed is wall-clock duration. User is CPU time in user mode, and sys is CPU time spent by the kernel on the command.

How can I time a pipeline in Bash?

Put Bash’s time keyword before the first command in the pipeline. GNU time can time a shell that runs the pipeline, such as /usr/bin/time sh -c ‘cmd1 | cmd2’.