The time command in Linux is like a stop watch built directly into your command line terminal. The time command is able to track how much time any command takes to finish executing. All you need to do is preface some command with the time command. Your command will execute as normal, but it will also show the duration of the command.
If you have ever needed to measure the duration of a command, this is the perfect tool to do so. This is a basic command with very few options and a simple purpose. So, it doesn’t take long to learn. In this tutorial, you’ll learn how to use it in Linux through examples. Follow along with our examples below to see how it works.
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
Frequently Used Options
The time command has a simple syntax and a very simple purpose, so it doesn’t take long to learn. The following examples will teach you everything you need to know about the time command, and the advanced section will show you some lesser used options that may come in handy every once in a while.
Note that in this guide we are mainly working with the Bash time command. GNU and other system shells like zsh have their own versions of the time command, which work slightly differently. Mainly, their output looks a little different.
time command in Linux Basic Examples
As mentioned earlier, simply preface any command with the time command to measure how long the command takes to execute. A perfect example would be with the wget command, which is used to download a file from the Internet. With the time command, we can effectively measure how long a file takes to download to our computer.
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. We can tell our system to use this specific implementation of the time command by supplying the full path to the command – /usr/bin/time.
This outputs the same information as time, along with some more detailed statistics, and a very human-readable measurement of the CPU usage.
Advanced Usage
The time command is pretty simple, and you will probably find that you don’t usually supply any extra options when running the command. However, there are a few different options that can supply with GNU time which will definitely come in handy in various situations. In this section of the guide, we’ll show you a few of the lesser known options that we think are useful.
time command in Linux Advanced Examples
Use the -o option to send the time output to a specified file instead of to standard error as it usually is.
In this case, the output from the time command is sent to download.log instead of to the terminal.
Use the -v (verbose) option to get very detailed output. Be warned that the output is very large, so you probably won’t find yourself using this option very often.
$ /usr/bin/time -v wget http://example.com/linux.iso
Command being timed: "wget https://wordpress.org/latest.tar.gz"
User time (seconds): 0.08
System time (seconds): 0.25
Percent of CPU this job got: 10%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:03.15
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 7112
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 426
Voluntary context switches: 455
Involuntary context switches: 76
Swaps: 0
File system inputs: 0
File system outputs: 29448
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
Closing Thoughts
In this guide, we learned all about the time command which is the only command you will need to know when you need to measure how long something takes to execute on a Linux system. This command is very simple, so usually you won’t need to run it with any extra options. However, we’ve shown you a few handy options in this guide which might prove useful in certain situations.