How to get statistics about a command execution with GNU time

GNU time is a really handy utility available in the repositories of every Linux distribution: we can use it to retrieve information about the “real”, “user”, and “system”  execution times of a command, and, more generally, to check the amount of system resources used by it.

In this tutorial we see how to install GNU time, and how to use it to retrieve information about a command execution.

In this tutorial you will learn:

  • How to install GNU time on the most used Linux distributions
  • How to use GNU time to retrieve statistics about the execution of a command
  • How to format and customize the output GNU time
how to get statistics about a command execution with gnu time
How to get statistics about a command execution with GNU time
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Distribution agnostic
Software GNU time
Other Privileged access to your Linux system as root or via the sudo command in order to perform system-wide installation of required packages
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

GNU time vs the “time” shell builtin

Modern versions of the Bash and ZSH shells, include a built-in “time” command which works similarly to GNU time, but is more limited. We can verify this by running:

$ type time

At least when working in Bash, the output of the command above should be:

time is a shell keyword

GNU time is a separate tool: most of the time it is installed and available as /usr/bin/time. Compared to the shell builtin, it provides more options and returns additional information, such as the average amount of memory or CPU used by a command. In this tutorial we focus on it.

Installation

We can easily install the standalone GNU time utility, since it is available in the official repositories of all the major Linux distributions. To perform the installation on Debian and Debian-based distributions we can run:

$ sudo apt install time



To installation the utility on Fedora and other distributions of the Red Hat family, instead:

$ sudo dnf install time

The utility is also available in the Archlinux “Extra” repository. As any other package, it can be installed with pacman:

$ sudo pacman -S time

How does “time” work?

The time utility runs a given command, and, when the latter exits, reports statistics about resources used by the latter. When we invoke the utility without any option, it returns a vast amount of information, such as, in order:

  1. The user CPU time
  2. The system CPU time
  3. The elapsed real time between the program invocation and termination
  4. The percentage of CPU usage
  5. Average size of the process’s shared text space
  6. Average size of the process’s unshared data area
  7. Maximum resident set size of the process during its lifetime
  8. Number of filesystem inputs by the process
  9. Number of filesystem outputs by the process
  10. Number of major page faults that occurred while the process was running
  11. Number of minor, or recoverable, page faults.
  12. Number of times the process was swapped out of main memory



The first three items in the list are returned also by the “time” shell builtin. The first two express, in seconds, the amount of time the CPU spent executing the program respectively in userspace and kernelspace. The third one, instead, is the “real” time elapsed from the command execution to its termination. If these three items are the only ones we are interested in, we can invoke the utility with the -p option. In the following example, we use GNU time to run “curl” and download the latest available Linux kernel release tarball:

$ /usr/bin/time -p curl -O https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.4.7.tar.xz

As soon as the download is completed, information are printed on the console standard error:

real 26.01
user 0.66
sys 0.89

Formatting the output of GNU Time

We can customize the output of the GNU time utility by using the -f option, passing a string containing the appropriate placeholders as argument. In the table below we see some of them, including those used to produce the default output of the command, as we saw above:

Placeholder Replaced by
%E Elapsed real time in “hours:minutes:seconds” format
%e Elapsed real time in seconds
%S System CPU time in seconds
%U User CPU time in seconds
%P Percentage of CPU used for a command
%K Average memory used by the process, expressed in Kbytes
%X Average size of the process’s shared text space, in Kbytes
%D Average size of the process’s unshared data area, in Kbytes
%M Maximum resident set size of the process during its lifetime, in Kbytes
%W Number of times the process was swapped out of main memory
%I Number of filesystem inputs by the process
%O Number of filesystem outputs by the process
%F Number of major page faults that occurred while the process was running. These are faults where the page has to be read in from disk
%R Number of minor, or recoverable, page faults. These are faults for pages that are not valid but which have not yet been claimed by other virtual pages. Thus the data in the page is still valid but the system tables must be updated.



To replicate GNU Time default output we can use the following format:

"%Uuser %Ssystem %Eelapsed %PCPU (%Xtext+%Ddata %Mmax)k
%Iinputs+%Ooutputs (%Fmajor+%Rminor)pagefaults %Wswaps"

The traditional, POSIX format, instead, is the equivalent of:

"real %f\nuser %f\nsys %f\n"

Redirecting the output of GNU Time

As we already said, by default, GNU time, writes to standard error. In certain cases, however, we may want to write the output of the utility to a file, overwriting or preserving existing content. In the first case it is enough to use the --output option (-o), and pass the path of the destination file as argument. Sticking to the previous example, to write statistics about the “curl” command to the timed_curl.txt file in the current working directory, we would run:

$ /usr/bin/time -p --output timed_curl.txt curl -O https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.4.7.tar.xz

To append to a file and preserve its current content, instead, in addition to the -o option, we must use --append (-a):

$ /usr/bin/time -p --append --output timed_curl.txt curl -O https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.4.7.tar.xz

Using GNU time by default

Since the Bash “time” builtin command is used as a default, to be able to invoke GNU time without specifying its full path, we can create an alias in our ~/.bashrc file (or our favorite shell equivalent):

alias time="/usr/bin/time"



The ~/.bashrc file is sourced every time an interactive Bash shell is opened, therefore, to make the change effective we can either open a new shell session, or source the file manually from the current one:

. ~/.bashrc

Conclusions

In this tutorial we learned how to retrieve statistics about the execution of a command by using the GNU time utility. We learned what is the difference between GNU “time” and the “time” shell builtin, how to format the output of the utility using string placeholders, and how to redirect it to a file. To know more about the utility and check the complete list of available placeholders, please consult the utililty manual.