How to use time command on Linux

The time command is a very simple, but useful command line utility in Linux. Essentially, you can think of it as a stopwatch built into the terminal, as it measures the amount of time it takes to execute a specified Linux command.

In this guide, we’ll show you how to use the time command through various examples, and teach you how to interpret its output. We’ll also show how to use GNU time, which is different than the time utility built into the Bash and zsh shells.

In this tutorial you will learn:

  • How to use time command
  • How to use GNU time
  • How to interpret the output of the time and GNU time commands
time command on Linux

time command on Linux

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software time
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

time vs GNU time



We’ll be discussing two different versions of time in this article, the default time for the Bash shell, and GNU time. Note that there are also other versions of time used on other shells, such as zsh. For the purposes of this guide, we’ll assume you’re using the Bash shell.

To call the Bash time utility, you can simply type the time command. To use GNU time, specify the full path which should be /usr/bin/time.

$ time (command here)
OR
$ /usr/bin/time (command here)

time and GNU time examples

In the following example, we’ll use both utilities to measure the time it takes to download a file with wget.

First, with Bash time:

Using Bash time command to measure the time it takes to download a file

Using Bash time command to measure the time it takes to download a file

The part we want to pay attention to is the last three lines, which were output by time.

real	4m12.067s
user	0m0.086s
sys	0m1.030s

Here’s what this information means:

  • real – the actual amount of time it took to run the command
  • user – the amount of time the CPU spent in user mode
  • sys – the amount of time the CPU spent in kernel mode


And now let’s try the same download, while measuring with GNU time:

Running the same download but measuring with GNU time

Running the same download but measuring with GNU time

We’ll only concern ourselves with the last two lines – the ones from GNU time.

0.05user 0.95system 0:08.64elapsed 11%CPU (0avgtext+0avgdata 7220maxresident)k
0inputs+30488outputs (0major+428minor)pagefaults 0swaps

This outputs the same information as time, along with some more detailed statistics, and a very human-readable measurement of the CPU usage.

Closing Thoughts

In this guide, we saw how to use the time and GNU time command line utilities on Linux to measure the amount of time and CPU usage it takes to execute any command we want. These commands are very simple to master, but can come in handy quite often.



Comments and Discussions
Linux Forum