The Linux time
command is a utility that is used to measure the execution time of a command or program. It can be useful for testing the performance of different commands or programs, or for identifying bottlenecks in the execution process.
The time
command is built into the Linux shell, and can be used by running time
followed by the command or program that you want to measure. For example, to measure the execution time of the ls
command, you could run:
time ls
When the time
command is run, it will display the execution time of the command or program in the following format:
real time
user time
sys time
The real
time is the total elapsed time that the command or program took to execute, including any time spent waiting for resources or input/output operations. The user
time is the amount of CPU time that was spent executing the command or program in user mode. The sys
time is the amount of CPU time that was spent executing the command or program in kernel mode.
The time
command has a number of options that can be used to customize its behavior. For example, you can use the -p
option to display the output in a portable format, or the -f
option to specify a custom format for the output. You can also use the --append
option to append the execution time to a file, rather than displaying it on the command line.
In addition to measuring the execution time of a command or program, the time
command can also be used to measure the resources that are consumed by the command or program. For example, you can use the --verbose
option to display the maximum resident set size (RSS) of the command or program, or the --output
option to specify a file to which the output should be written.
Linux Time Command Examples
Display the output in a portable format: To display the output of the time
command in a portable format, you can use the -p
option. This can be useful if you want to parse the output of the time
command using a script or program. For example:
time -p ls
This will display the execution time of the ls
command in the following format:
real time
user time
sys time
Specify a custom format for the output: To specify a custom format for the output of the time
command, you can use the -f
option. This can be useful if you want to customize the output of the time
command to include only specific information. For example:
time -f "Elapsed time: %E" ls
This will display the elapsed time (%E
) of the ls
command in the following format:
Elapsed time: time
Append the execution time to a file: To append the execution time of a command or program to a file, rather than displaying it on the command line, you can use the --append
option. For example:
time --append -o
The time
command is a useful tool for measuring the execution time and resource consumption of different commands or programs in Linux. It can be used to troubleshoot performance issues, or to optimize the performance of different commands or programs.