Linux Time Command
In Linux, the time command displays the length of time it takes for a command to execute. It's used in conjunction with other commands to figure out how long it takes to perform a task.
The time command helps evaluate script performance. The amount of time it takes to complete a task is an essential indicator in assessing performance.
Using the command time
In Linux, you may use the time command by typing 'time' before the command you wish to run. Let's examine how long it takes to show the ls command output:
$ ls time
We obtain three distinct sorts of times as a result of the output. In a moment, we'll get to them.
Let's try something else:
ping linuxfordevices.com with $ time
The output of the time command follows the output of the command we're using to execute it.
In the end, there are three sorts of times: actual, user, and sys.
Real: This is the amount of time it takes from the moment the call is placed to when it is finished. When measured in real-time, this represents the amount of time elapsed.
User: This is the CPU's percentage of time spent in user mode.
Sys: The amount of time the CPU spends in kernel mode. System time is abbreviated as Sys.
In Linux, there are many different versions of the time command.
There are many variations of the time command. Three, to be exact. These are the following:
Bash
Zsh
It's GNU time (sh)
You can tell which one you're using by looking at:
$ time to type
You'll receive the following result depending on whatever version you're using:
BASH
The term "time" is a shell keyword.
ZSH
The term "time" has a special meaning for me.
It's GNU time.
/usr/bin/time is the time.
Time Period
If Bash and Zsh are installed, their built-in versions take priority over the GNU time command. We'll have to indicate GNU time if we explicitly utilise it. This is the next thing we'll look at.
1. The time command in GNU (Default)
To use the time command in GNU, type:
/usr/bin/time /usr/bin/time /usr/bin/time
The GNU time command provides a more thorough output.
$ ls /usr/bin/time
One of the advantages of the GNU time command is that it is simple to experiment with. Use the -v option with the time command to show output more verbosely.
/usr/bin/time -v ls /usr/bin/time -v ls /usr/bin/time
Linux Time Command - Verbose Output
The GNU time command allows you to format and save the results to a file. This is the next thing we'll look at.
2. Writing the results to a file
Use the -o argument with the GNU time command to save the output to a file. This may be done by issuing the following command:
ls /usr/bin/time -o time output.txt $ /usr/bin/time -o time output.txt
The command will create a file called time output.txt, in which the output will be saved.
Use the command to attach the output to the end of an existing file:
ls /usr/bin/time -a /home/smart/time-output.txt $ /usr/bin/time -a /home/smart/time-output.txt
3. GNU time output formatting
The GNU time command also can be structured and rendered in a more user-friendly way. The -f parameter is used with the time command to format the output.
The GNU time command has several resource specifiers that may be used in it.
Use the following command to acquire a list of resource specifiers:
/usr/bin/time $ man
GNU Resource Specifiers for Time
This may be put to use in the following way:
$ time -f /usr/bin/time "t% C [Command name], t% K [Total memory consumption], t% E [Time Elapsed]" journaldev.com is pingable.
t inserts a tab between the output lines.
Conclusion
We showed how to use the time command and the default form of the command. It is relatively usual to use the time command to evaluate the execution of commands/scripts. Refer to the man page for the time command for further information.