Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

The time command in Linux measures the execution time of an executable. The executable can be a command, a program or a script.

The time command prints its output to the standard error, which is the terminal window by default. We may need to redirect the output of the time command to a file and use it later.

In this tutorial, we’ll discuss how to redirect the output of the time command to a file.

2. The Problem in Redirection

Let’s try using the time command for measuring how long a command lasts:

$ time sleep 2

real    0m2.003s
user    0m0.001s
sys     0m0.002s

The actual time spent for running the sleep 2 command took 2.003 seconds when we ran the time sleep 2 command. The output of the time command also includes the CPU seconds the executable uses in the user and the kernel modes.

So, in the above example, the CPU time spent in the user mode was 0.001 seconds and the CPU time spent in the kernel mode was 0.002 seconds.

Now, let’s rerun the same example, but redirecting the standard error of the time command to a file:

$ time sleep 2 2> out.time

real    0m2.005s
user    0m0.002s
sys     0m0.002s

As we see, although we redirected the standard error to the file out.time, the output of the time command was still printed in the terminal window. Let’s check the content of the out.time file:

$ cat out.time
$

The file is empty in spite of the redirection of the standard error. The reason for this is that the time command interprets the 2> out.time as a part of the sleep command as if we want to measure the execution time of the sleep 2 2>out.time command. Since the command sleep 2 doesn’t print anything, the file is empty.

3. The time Built-in Command

3.1. Putting time in a Group

As a solution for properly redirecting the output of the time command to a file, we can group the time command with curly braces and redirect the output of the group:

$ { time sleep 2; } 2> out.time
$ cat out.time

real    0m2.002s
user    0m0.002s
sys     0m0.000s

Now, we achieved our goal. The reason for grouping the time command is to isolate the redirection part, 2> out.time, from the time command. As a result of the isolation, the redirection part isn’t evaluated as a part of the command passed to the time command.

Grouping commands using curly braces runs the commands between curly braces in the current shell. The semicolon at the end of the commands is necessary, otherwise we get a syntax error.

3.2. Preventing All Errors Reaching Our Output File

The above solution comes with a downside. If the executable passed to the time command prints something to the standard error, then the output of both the time command and the executable will be written to the same file. As an example, let’s say we have a script, sleepy_hello_world.sh:

#!/bin/bash

echo "Hello World to Standard Output" >&1
echo "Hello World to Standard Error" >&2

sleep 2

This script prints the outputs of the first and second echo commands to the standard output and the standard error, respectively. Then it sleeps for two seconds.

Let’s run this script by grouping the time command with curly braces:

$ { time sleepy_hello_world.sh; } 2> out.time
Hello World to Standard Output
$ cat out.time
Hello World to Standard Error

real    0m2.006s
user    0m0.005s
sys     0m0.001s

As we see, the output of the second echo command in the script and the output of the time command were written to the same file. However, we can separate the outputs by diverting the stderr of the command we’re timing to a file:

$ { time sleepy_hello_world.sh 2> out.script; } 2> out.time
Hello World to Standard Output
$ cat out.script
Hello World to Standard Error
$ cat out.time

real    0m2.006s
user    0m0.002s
sys     0m0.004s

4. The /usr/bin/time Command

The time command we’ve used up to this point is the built-in time command provided by Bash. In fact, there is one more time command that we can use.

Let’s check the available time commands using the type command:

$ type -a time
time is a shell keyword
time is /bin/time
time is /usr/bin/time

The first time command, /bin/time, is the built-in time command that we have been using:

$ which time
/bin/time

As an alternative to the built-in time command, we can use the second time command, /usr/bin/time. It also takes an executable as a parameter, runs it and displays the execution time at the end:

$ /usr/bin/time -p sleep 2
real 2.00
user 0.00
sys 0.00

The –p option of the /usr/bin/time command is for formatting the output. It also has another option that’s of interest to us, the –o option. This option takes a file name. We can write the output of the /usr/bin/time command to the file specified with the –o option:

$ /usr/bin/time -p -o out.time sleep 2
$ cat out.time
real 2.00
user 0.00
sys 0.00

That’s what we want. The output of the executable passed to the /usr/bin/time command doesn’t get mixed with the output of the /usr/bin/time command either:

$ /usr/bin/time -p -o out.time sleepy_hello_world.sh
Hello World to Standard Output
Hello World to Standard Error
$ cat out.time
real 2.00
user 0.00
sys 0.00

We can view the manual page of the /usr/bin/time command using man time. We can get information about the built-in time command using help time.

5. Conclusion

In this tutorial, we discussed two alternative ways for redirecting the output of the time command.

First we discussed the built-in time command, we used command grouping for redirection. Then we discussed the /usr/bin/time command which has the –o option for redirecting the output to a file.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!