Linux Time Command

Introduction

The time command is used to find out how long a command will take to execute. It's useful for seeing how well your scripts and commands perform.

For example, if you have two scripts that accomplish the same thing and want to see which one performs better, you may use the Linux time command to see how long each one takes to run.

In this tutorial we will provide an overview of the working of linux time command.

Time Command Versions

The most popular Linux shells, Bash and Zsh, both feature built-in versions of the time command that take precedence over the Gnu time command.

To find out whether time is a binary or a built-in keyword, use the type command.

type time
Output

# Bash
time is a shell keyword

# Zsh
time is a reserved word

# GNU time (sh)
time is /usr/bin/time

To use the Gnu time command, you must either specify the complete path to the time binary, which is generally /usr/bin/time, use the env command, or use a leading backslash \time, which disables the usage of both and built-ins.

Gnu time allows you to format the output and gives additional information such as memory I/O and IPC calls.

Using Linux Time Command

We'll use the wget program to measure the time it takes to download the Linux kernel in the following example:

time wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.19.9.tar.xz

The output will vary depending on the version of the time command you're using:

Output

# Bash
real	0m33.961s
user	0m0.340s
sys	0m0.940s

# Zsh
0.34s user 0.94s system 4% cpu 33.961 total

# GNU time (sh)
0.34user 0.94system 0:33.96elapsed 4%CPU (0avgtext+0avgdata 6060maxresident)k
0inputs+201456outputs (0major+315minor)pagefaults 0swaps
  • real or total or elapsed (wall clock time) is the duration of the call from beginning to end. It refers to the time between pressing the Enter key and the completion of the wget command.
  • user - time spent in user mode on the CPU.
  • system or sys - time spent in kernel mode on the CPU.

FAQs on Linux Time Command

How do I measure the execution time of a command?

To measure the execution time of a command, prefix it with the time command. For example: time ls -l.

What information does the time command provide?

The time command provides information such as the real (wall-clock) time, user CPU time, and system CPU time taken by the command.

What does real time mean in the time command output?

Real time refers to the actual elapsed time the command took to execute, including any time spent waiting for system resources or blocked I/O operations.

What is user CPU time in the time command output?

User CPU time represents the amount of CPU time consumed in user-mode by the command or program.

How can I save the output of the time command to a file?

You can save the output of the time command to a file by redirecting both stdout and stderr to a file. For example: time ls -l > output.txt 2>&1.

Can the time command show the time taken by each subprocess in a pipeline?

No, the time command shows the cumulative time taken by a pipeline as a whole, rather than the time taken by each individual subprocess.

Is there a way to suppress the default output of the time command?

Yes, you can redirect the stderr of the time command to /dev/null to suppress the default output. For example: time ls -l 2>/dev/null.

Conclusion

You should now have a firm grasp on how to utilize the time command. Visit the time man page for more information on the Gnu time command.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.