Linux time command is helpful to identify the time taken by a command.
Using Linux time command, you can figure out how much time was taken to execute a command, or shell script, or any other program.
By default, time command executes the given command or program. After execution, it displays the statistics and resources usage on the standard error.
Time command provides several command line options and various format options as explained in this tutorial
1. Basic Time Command Usage Example
Time command syntax:
$ time [-options] <command arg1 arg2 ..>
For example, time command is run on sleep command without any option.
$ /usr/bin/time sleep 2 0.00user 0.00system 0:02.00elapsed 0%CPU (0avgtext+0avgdata 2288maxresident)k 0inputs+0outputs (0major+172minor)pagefaults 0swaps
Let’s now understand some important command line options of this command.
2. Write Time Statistics Output to a File using -o option
This option is to restrict sending the command results to standard error, but writes results into output file. This option overwrites the specified file.
Here is an example :
$ /usr/bin/time -o time.txt sleep 2 $ cat time.txt 0.00user 0.00system 0:02.00elapsed 0%CPU (0avgtext+0avgdata 2288maxresident)k 0inputs+0outputs (0major+175minor)pagefaults 0swaps
3. Append Time Statistics Output to an Existing File using -a option
This option allows appending time command output into file. It is used along with -o option. This option avoids overwriting of content of output file by appending time command output into specified output file.
Here is an example :
$ /usr/bin/time -a -o time.txt sleep 4 $ cat time.txt 0.00user 0.00system 0:02.00elapsed 0%CPU (0avgtext+0avgdata 2288maxresident)k 0inputs+0outputs (0major+175minor)pagefaults 0swaps 0.00user 0.00system 0:04.00elapsed 0%CPU (0avgtext+0avgdata 2288maxresident)k 0inputs+0outputs (0major+176minor)pagefaults 0swaps
4. Display Percentage of CPU used – %P
You can provide output formatting choices using -f option. This option allows user to provide output formatting options. It possibly does overriding the format specified in the environment variable TIME. Following formatting options are described below – %P, %M, %S, %e, %E, %C, %Z, %c, %x.
This option gives percentage of the CPU that the process of command (i.e. run under time command) has got for its execution. This is just user + system times divided by the total running time. It also prints a percentage sign.
$ /usr/bin/time -f "\t%P CPU Percentage" find / -name my-program.sh /root/my-program.sh 82% CPU Percentage
Here, command output shows that find command took 82% of CPU.
5. Display Maximum Resident Set Size – %M
This option gives maximum resident set size of the process of command (i.e. run under time command) during its lifetime, in Kilobytes.
$ /usr/bin/time -f "\t%M Max Resident Set Size (Kb)" find / -name my-program.sh /root/my-program.sh 8688 Max Resident Set Size (Kb)
Here, command output shows that find command took 8688 KB as maximum resident size of process.
6. Display Total Number of CPU-seconds – %S
This option gives total number of CPU-seconds used by the system on behalf of the process (in kernel mode), in seconds.
$ /usr/bin/time -f "\t%S CPU-seconds" find / -name my-program.sh /root/my-program.sh 0.35 CPU-seconds
Here, command output shows that find command took 0.35 CPU-seconds in kernel mode.
7. Display Elapsed Real Time in Seconds – %e
This option gives elapsed real time (i.e. wall clock) used by the process, in seconds.
$ /usr/bin/time -f "\t%e Elapsed Real Time (secs)" sleep 2 2.00 Elapsed Real Time (secs)
Here, command output shows that sleep command execution elapsed till 2 seconds.
8. Display Elapsed Real Time in Different Format – %E
This option gives elapsed real time (i.e. wall clock) used by the process, in this format – [hours:]minutes:seconds.
$ /usr/bin/time -f "\t%E Elapsed Real Time (format)" sleep 2 0:02.00 Elapsed Real Time (format)
Here, command output shows that sleep command execution took 0 hour, 0 minute and 2 seconds.
9. Display Program Name and Command Line Arguments – %C
This option gives name and command line arguments of the command (i.e. run under time command).
$ /usr/bin/time -f "\t%C (Program Name and Command Line)" find / -name my-program.sh /root/my-program.sh find / -name my-program.sh test_time (Program Name and Command Line)
Here, time command output shows name of command being run and its command line arguments.
10. Display System Page Size in Bytes – %Z
This option gives system’s page size, in bytes. This is a per-system constant, but it may vary one system to another system.
$ /usr/bin/time -f "\t%Z System Page Size (bytes)" sleep 2 4096 System Page Size (bytes)
Here, command output shows that sleep command used 4096 bytes as system page size.
11. Display Number of Context Switches – %c
This option gives number of times the process was context-switched involuntarily (because the time slice expired).
$ /usr/bin/time -f "\t%c Context Switches" find / -name my-program.sh /root/my-program.sh 254 Context Switches
Here, command output shows that 254 times context switching of process took place during execution of find command under time command.
12. Display Exit Status of a Command – %x
This option gives exit status of the command (i.e. run under time command).
$ /usr/bin/time -f "\t%x Exit Status" top1 /usr/bin/time: cannot run top1: No such file or directory 127 Exit Status
Here, command output shows that top1 command is failed because this tope1 as a file does not exist.
As per man page of time command, exit status of time command can be following:
- If command specified to time command was invoked, the exit status is the exit status of command which is run with time command.
- It is 127 if command specified to time command could not be found.
- 126 if command specified to time command could be found but could not be invoked.
- Some other non-zero value (1-125) if something else went wrong.
Finally, there is a difference between executing just “time” and “/usr/bin/time”. We explained this in our earlier introduction to time command article.
Comments on this entry are closed.
Really useful post. I used it today and helped a lot. Thanks!!!
In example 8, I think the format is differrent in any Linux Distro I use. It shows hundreds of a second as well. Maybe your distro is diferent?
i.e. hh:mm:ss.ss.
Very helpful article. I use the %E with my rsync scripts regularly to monitor for problems and needs for tuning.
I am getting an error when I run this command:
$time -o time.txt sleep 2
-o: command not found
real 0m0.069s
user 0m0.056s
sys 0m0.008s
This however works fine:
$ /usr/bin/time -o time.txt sleep 2
$ which time
/usr/bin/time
Why do I have use full path when referencing the command? Bug?
These commands are very useful.
Hi,
Thanks a lot…
Simple and useful article….
@Jacek
Bash has a built-in time command that does NOT support formatting options. If you do not provide the full path name it will be used rather than the advanced time command in the examples.
This implies you need to make sure you have the external time command installed as well. Some distros do not do that by default.
Dan.
thank u..its so helpful for me to learn