time Command in Linux



The time command in Linux runs a command or program and summarizes the system resource usage. It displays the real time, user time, and system time taken by the command to execute. It helps to assess performance, optimize tasks, and identify bottlenecks. It is essential for benchmarking, troubleshooting, and resource management.

Table of Contents

Here is a comprehensive guide to the options available with the time command in Linux −

Syntax of time Command

The syntax of the time command in Linux is as follows −

time [options…] command [args…]

In the above syntax, the [options] field is used to specify the optional flags and options. The command field is used to specify the command to process, and [args...] is used to specify the command arguments.

Note that bash or zsh uses its built-in time keyword, which does not support various options. Those options are provided by the GNU time program, usually located at /usr/bin/time.

To determine whether the time is a shell keyword or a binary by default, use the following command −

type time
time Command in Linux1

If time is a shell keyword, it can be bypassed by using the full path to the binary, like this −

/usr/bin/time [options] command [args…]

Options of time Command

The options for the Linux time command are listed below −

Short Option Long Option Description
-f format --format= format Specify output format.
-p --portability Use portable output format.
-o file --output= file Write results to the specified file.
-a --append Append results to the file.
-v --verbose Print detailed information.
-q --quiet Suppress abnormal termination messages.
--help Show help message and exit.
-V --version Display version info and exit.
-- End of option list.

Using time Command in Linux

This section explores how to use the time command in Linux with examples −

Displaying the Execution Time of a Command

To print the execution time of a command, use the time command followed by the command to be processed −

time sleep 5

The above command measures the real-time, user time, and system time as shown in the output image below −

time Command in Linux2

In the above output image −

  • real − The total elapsed time, also known as wall-clock time.
  • user − The amount of time the CPU spent in user mode (running the program's code).
  • sys − The amount of time the CPU spent in kernel-mode (system calls and kernel operations)

To get the execution time, using the GNU time, run the following command −

/usr/bin/time sleep 5
time Command in Linux3

The GNU command shows that the sleep ran for 5 seconds with no CPU usage, minimal memory (1792 KB), no I/O, and 86 minor page faults.

Displaying Output in Portable Format

To display the output in a more consistent, portable format across different systems, use the -p or --portability option −

time -p sleep 5
time Command in Linux4

Now, run it using the GNU time command −

/usr/bin/time -p sleep 5
time Command in Linux5

The portable output is useful for scripts as it is easier to parse.

Saving Output to a File

To save the output to a file, use the -o or --output option followed by the filename −

/usr/bin/time -o output.txt sleep 5

To view the output, use the cat command followed by the output filename as shown in the following output image −

time Command in Linux6

To append the execution time to an existing file, use the -a or --append option −

/usr/bin/time -a -o output.txt pwd
time Command in Linux7

Customizing the Output Format

To customize the output format, use the -f or --format option. For example, to display only the real-time elapsed −

/usr/bin/time -f "%E real time" ls
time Command in Linux8

Getting Verbose Output

To enable the verbose mode, use the -v or --verbose option −

/usr/bin/time -v sleep 5
time Command in Linux9

Suppressing Errors

To get a quiet output and suppress errors, use the -q or --quiet option −

/usr/bin/time -q sleep 5

Displaying Download Time

To get the download time of a file, use the time command in the following way −

/usr/bin/time -o output.txt wget
https://filesamples.com/samples/ebook/epub/Around%20the%20World%20in%2028%20Languages.epub
time Command in Linux10

Displaying Usage Help

To display the usage help for the time command, use the --help option −

/usr/bin/time --help

Conclusion

The time command in Linux is a useful tool for measuring how long a command or program takes to run, showing real, user, and system times. It helps analyze performance, troubleshoot issues, and manage system resources effectively. With various options to format, save, or suppress output, it offers flexibility for different needs, from basic timing to detailed reporting.

Advertisements