Open In App

time command in Linux with examples

Last Updated : 26 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

time command in Linux is used to execute a command and prints a summary of real-time, user CPU time and system CPU time spent by executing a command when it terminates. ‘real‘ time is the time elapsed wall clock time taken by a command to get executed, while ‘user‘ and ‘sys‘ time are the number of CPU seconds that command uses in user and kernel mode respectively. 

Understanding Time Command Basics

The time command measures the execution time of a specified command or program and reports various metrics, including real, user, and system time. Here’s a breakdown of these metrics:

  • Real Time: The actual elapsed time, from start to finish, including time spent waiting for I/O and other processes.
  • User Time: The amount of CPU time spent executing user-mode instructions within the process.
  • System Time: The amount of CPU time spent executing system-level instructions on behalf of the process.

Basic Usage:

The syntax for using the time command is straightforward:

time [options] command [arguments]

Options Available in Time Command in Linux: 

time -p : This option is used to print time in POSIX format. 

help time : it displays help information. 

Examples of Time Command in Linux

To Create a Dummy Job with time Command:

In this , sleep 3 is used to create a dummy job which lasts 3 seconds.

time sleep 3

In the above example, sleep 3 is used to create a dummy job which lasts 3 seconds. 

Measure Execution Time of a Command:

time wget http://example.com/file.zip

This example demonstrates how to use the time command to measure the execution time of a single command. In this case, the command wget http://example.com/file.zip is timed, and the real, user, and system times are reported upon completion. This is useful for evaluating the performance of individual commands, such as downloading a file from a remote server.

Measure Execution Time of a Shell Script:

time ./my_script.sh

Here, the time command is used to measure the execution time of a shell script named my_script.sh. When executed, time will run the shell script and provide timing statistics upon completion. This is helpful for analyzing the performance of complex operations or tasks encapsulated within shell scripts.

Compare Execution Time of Multiple Commands:

time { command1 ; command2 ; command3 ; }

In this example, multiple commands (command1, command2, and command3) are enclosed within curly braces and executed sequentially. The time command is used to measure the combined execution time of all the commands enclosed within the braces. This allows for easy comparison of the performance of multiple commands executed in sequence.

Redirect Output to a File:

time -o timing.log ls -l

Here, the time command is used with the -o option to redirect the timing data to a file named timing.log. This is useful for capturing timing statistics for further analysis or documentation purposes. In this example, the ls -l command is timed, and the timing data is written to the specified file.

Custom Output Format:

time -f "User: %U seconds, System: %S seconds, Real: %e seconds" command

This example demonstrates how to specify a custom output format using the -f option with the time command. The format string "User: %U seconds, System: %S seconds, Real: %e seconds" defines the desired format for the timing data, including user, system, and real times. This allows for flexibility in formatting the output according to specific requirements or preferences

Conclusion

In this article we discussed the time command in Linux which is super helpful for figuring out how long it takes for commands or programs to run. It tells you stuff like the actual time it took (real time), how much CPU time it used (user and system time), and helps you see if something’s running efficiently. Understanding the basics, like the syntax and options, is key. Through examples, we’ve shown how to use time to measure command and script execution, compare multiple commands, and even customize the output format. By using time, Linux users can better understand and improve their system’s performance.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads