Linux / Unix: time Command Examples

I am new Linux and Unix systems user. How do I display the time of the execution of a command/script on Linux or Unix-like operating systems using shell prompt?

You need to use the time command to display the elapsed time during the execution of a command or script. This page explains time command in Linux and Unix-like system with most common examples.

time command details
DescriptionReport time consumed by command execution
CategoryProcesses Management
Difficulty levelEasy
Root privilegesNo
OS compatibilityBSD Linux macOS Unix WSL
Est. reading time7 minutes
The time command show the following information on screen (on the standard error output, by default) about resources used by command:

  1. real time
  2. user time
  3. sys time

Purpose

Run command/programs or script and summarize system resource usage on your screen.

Syntax

The basic syntax is as follows:
$ time command
$ time command arg1 arg2 ... argN
$ time [options] command arg1 arg2 ... argN

Is my time command shell built-in or external command?

The time command is also built into the BASH/KSH/CSH/TCSH with a different syntax. Let us use the type command or command command to verify location:
$ type -a time
$ command -V time

Here is what I see:

time is a shell keyword
time is /usr/bin/time
time is /bin/time

Calling external time command using a full path

Users of the bash or ksh shell need to use an explicit path in order to run the external time command and not the shell builtin variant. To run the time command while in the shells, type:
$ /usr/bin/time -p command
$ /bin/time -p command arg1 arg2

time command examples

To measure the time required to run a program called date, enter:
$ time date
To use external time command give full path to time binary:
$ /usr/bin/time -p date

How do I redirect time command output to a file?

The syntax is as follows to save a record of the time command information in a file called output.time.txt, run:

time date 2> output.time.txt
/usr/bin/time -p date 2> output.time.txt

If above command failed, try the following to save a record of the time command information in a file:

( time date ) 2> output.time.txt
## OR ##
{ time date ; } 2> output.time.txt

Use the cat command to display output on screen:
$ cat output.time.txt

A note about GNU/Linux time command

GNU/Linux user can use the following syntax to write the resource use statistics to file instead of to the standard error stream:
$ /usr/bin/time -o output.time.txt -p date
$ cat output.time.txt

Pass the -a option to append the resource use information to the output file instead of overwriting it. This option is only useful with the -o option:
$ /usr/bin/time -a -o output.time.txt -p sleep 2
$ cat output.time.txt

Using time command on my Linux system

You can control time command output format using -f FORMAT as follows:
$ /usr/bin/time -f 'FORMAT' -p command

Do you want the GNU time command on macOS? Install homebrew to use the brew command and then type the brew command:
$ brew install gnu-time

Use FORMAT as the format string that controls the output of time

Table 1: Formatting the output of /usr/bin/time
FORMAT Description
 %  A literal `%’.
 C  Name and command line arguments of the command being timed.
 D  Average size of the process’s unshared data area, in Kilobytes.
 E  Elapsed real (wall clock) time used by the process, in [hours:]minutes:seconds.
 F  Number of major, or I/O-requiring, page faults that occurred while the process was running. These are faults where the page has actually migrated out of primary memory.
 I  Number of file system inputs by the process.
 K  Average total (data+stack+text) memory use of the process, in Kilobytes.
 M  Maximum resident set size of the process during its lifetime, in Kilobytes.
 O  Number of file system outputs by the process.
 P  Percentage of the CPU that this job got. This is just user + system times divided by the total running time. It also prints a percentage sign.
 R  Number of minor, or recoverable, page faults. These are pages that are not valid (so they fault) 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.
 S  Total number of CPU-seconds used by the system on behalf of the process (in kernel mode), in seconds.
 U  Total number of CPU-seconds that the process used directly (in user mode), in seconds.
 W  Number of times the process was swapped out of main memory.
 X  Average amount of shared text in the process, in Kilobytes.
 Z  System’s page size, in bytes. This is a per-system constant, but varies between systems.
 c  Number of times the process was context-switched involuntarily (because the time slice expired).
 e  Elapsed real (wall clock) time used by the process, in seconds.
 k  Number of signals delivered to the process.
 p  Average unshared stack size of the process, in Kilobytes.
 r  Number of socket messages received by the process.
 s  Number of socket messages sent by the process.
 t  Average resident set size of the process, in Kilobytes.
 w  Number of times that the program was context-switched voluntarily, for instance while waiting for an I/O operation to complete.
 x  Exit status of the command.

Using time command on Linux or Unix with formatting

In this example, show just the user, system, and total time using format option:
$ /usr/bin/time -f "%E real,%U user,%S sys" sleep 2
$ /usr/bin/time -f "%E real,%U user,%S sys" /path/to/script

Sample outputs:

0:02.00 real,0.00 user,0.00 sys

See percentage of CPU used by your command:
$ /usr/bin/time -f "CPU Percentage: %P" command
$ /usr/bin/time -f "CPU Percentage: %P" grep vivek /etc/passwd
$ /usr/bin/time -f "CPU Percentage: %P" find /etc/ -type f -iname "a*.conf"

CPU Percentage: 76%

Understanding TIMEFORMAT used by bash’s buitin time

The value of TIMEFORMAT parameter is used as a format string specifying how the timing information for pipelines prefixed with the time reserved word should be displayed. The % character introduces an escape sequence that is expanded to a time value or other information. The escape sequences and their meanings are as follows; the braces denote optional portions.

Table 2: TIMEFORMAT parameter values
Value Description
%% A literal %.
%[p][l]R The elapsed time in seconds.
%[p][l]U The number of CPU seconds spent in user mode.
%[p][l]S The number of CPU seconds spent in system mode.
%P The CPU percentage, computed as (%U + %S) / %R.
The optional p is a digit specifying the precision, the number of fractional digits after a decimal point. A value of 0 causes no decimal point or fraction to be output. At most three places after the decimal point may be specified; values of p greater than 3 are changed to 3. If p, is not specified, the value 3 is used. The optional l specifies a longer format, including minutes, of the form MMmSS.FFs. The value of p determines whether or not the fraction is included.

How to use bash’s builtin time command in Linux / Unix

The syntax is almost same:
$ time command
$ time ls
$ TIMEFORMAT="%P" time ls
$ TIMEFORMAT="%U" time sleep 3
$ TIMEFORMAT="%S" time sleep 3
$ TIMEFORMAT="%R" time sleep 2

Summing up

You learned about the time command that reports the time consumed by the pipeline’s execution. It will summarize system resource usage on the screen. The syntax of time might change on your local Unix or Linux distro. Hence, to get help for builtin time command, type the following help command:
$ help time
# or read shell man page
$ man bash

For external time (/bin/time or /usr/bin/time) command, run the following man command:
$ man time

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Vivek Gite is an expert IT Consultant with over 25 years of experience, specializing in Linux and open source solutions. He writes about Linux, macOS, Unix, IT, programming, infosec, and open source. Follow his work via RSS feed or email newsletter.

Category List of Unix and Linux commands
AnsibleCheck version Fedora FreeBSD Linux Ubuntu 18.04 Ubuntu macOS
Archivingz commands
Backup ManagementDebian/Ubuntu FreeBSD RHEL
Database ServerBackup MySQL server MariaDB Galera cluster MariaDB TLS/SSL MariaDB replication MySQL Server MySQL remote access
Download managerswget
Driver ManagementLinux Nvidia driver lsmod
Documentationhelp mandb man pinfo
Disk Managementdf duf ncdu pydf
File Managementcat cp less mkdir more tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04 Ubuntu 24.04
KVM VirtualizationCentOS/RHEL 7 CentOS/RHEL 8 Debian 9/10/11 Ubuntu 20.04
Linux Desktop appsChrome Chromium GIMP Skype Spotify VLC 3
LXDBackups CentOS/RHEL Debian 11 Fedora Mount dir Ubuntu 20.04 Ubuntu 22.04
Modern utilitiesbat exa
Network ManagementMonitoring tools Network services RHEL static IP Restart network interface nmcli
Network UtilitiesNetHogs dig host ip nmap ping
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 11 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Power Managementupower
Package Managerapk apt-get apt yum
Processes Managementbg chroot cron disown fg glances gtop iotop jobs killall kill pidof pstree pwdx time vtop
Searchingag egrep grep whereis which
Shell builtinscompgen echo printf
System Managementreboot shutdown
Terminal/sshsshpass tty
Text processingcut rev
Text Editor6 Text editors Save and exit vim
User Environmentexit who
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami w
User Management/etc/group /etc/passwd /etc/shadow chsh
Web ServerApache Let's Encrypt certificate Lighttpd Nginx Security Nginx
WireGuard VPNAlpine Amazon Linux CentOS 8 Debian 10 Firewall Ubuntu 20.04 qrencode
4 comments… add one
  • Revanth kumar Jan 15, 2015 @ 0:56

    Why are we using /usr/bin/time? when not using any options simply writing time is working but when should we go for /usr/bin/time when using options? This is working but i dont understand the logic. when :
    time -o output.txt ls //is used i am getting error as no bash command -o
    /usr/bin/time -o output.txt ls //is working fine

    • Revanth kumar Jan 15, 2015 @ 1:03

      Got the answer. time simply referes to time command of bash which has no options. /usr/bin/time referes to the time command which this tutorial is speaking about. bash time command help: help time, normal time command help: man time

      • Andrew Grasso May 24, 2021 @ 20:28

        Thank you! I was scratching my head over this one.

  • sudhakar Aug 28, 2020 @ 10:54

    I use for bench marking, say, browsers:

    $cat cost.sh
    /usr/bin/time -f '------------------\nPrice of %C:\nKB %M\nSwitches %w\n------------------' $@

    Price of firefox:
    KB       117300
    Switches 1636
    

Leave a Reply

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.
close