If you want to be a good Linux administrator, therefore you should know
time command. It is used to determine how long a given command takes to run.
It is useful for testing the performance of your scripts and commands (ie helps to find execution time for shell scripts or time taken for a command to finish).
How to Use Linux time command
time command, just execute
time with the command/program you want to run as input. Please check below example
$ time ping linoxide.com
output PING linoxide.com (126.96.36.199) 56(84) bytes of data. 64 bytes from 188.8.131.52 (184.108.40.206): icmp_seq=1 ttl=58 time=1.77 ms 64 bytes from 220.127.116.11 (18.104.22.168): icmp_seq=2 ttl=58 time=2.12 ms 64 bytes from 22.214.171.124 (126.96.36.199): icmp_seq=3 ttl=58 time=1.65 ms --- linoxide.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 10016ms rtt min/avg/max/mdev = 1.118/1.476/2.124/0.362 ms real 0m10.536s user 0m0.002s sys 0m0.007s
real signifies the wall clock time the 'ping' command took from execution till termination,
sys are the time taken by
ping the user space and kernel space.
How to Make time command writes its output to a file
To write the time command output to a file instead of the print out screen, use the
-o command line option, which expects a file name/path as input.
$ /usr/bin/time -o /home/smart/time-output.txt ping linoxide.com
Now we will display ping's output on stdout, while the
time command output will be written to the text file.
Note: We used /usr/bin/time instead of
time because the shell built-in time command doesn't offer the
How to appends its output to an existent file
To append the time command output to an existent file instead of overwrite it, use the
-a command line option.
$ /usr/bin/time -a /home/smart/time-output.txt ping linoxide.com
How to get Detailed output of linux time command
We can use the
-v command line option to produce detailed output.
$ time -v ping linoxide.com
output Command being timed: "ping linoxide.com" User time (seconds): 0.00 System time (seconds): 0.00 Percent of CPU this job got: 0% Elapsed (wall clock) time (h:mm:ss or m:ss): 0:11.77 Average shared text size (kbytes): 0 Average unshared data size (kbytes): 0 Average stack size (kbytes): 0 Average total size (kbytes): 0 Maximum resident set size (kbytes): 3064 Average resident set size (kbytes): 0 Major (requiring I/O) page faults: 0 Minor (reclaiming a frame) page faults: 158 Voluntary context switches: 14 Involuntary context switches: 0 Swaps: 0 File system inputs: 0 File system outputs: 0 Socket messages sent: 0 Socket messages received: 0 Signals delivered: 0 Page size (bytes): 4096 Exit status: 0
How to customizing time command output
There are a large number of formatting options as shown in the following list
C - Name and command line arguments used
D - Average size of the process's unshared data area in kilobytes
E - Elapsed time in a clock format
F - Number of page faults
I - Number of file system inputs by the process
K - Average total memory use of the process in kilobytes
M - Maximum resident set the size of the process during the lifetime in Kilobytes
O - Number of file system outputs by the process
P - Percentage of CPU that the job received
R - Number of minor or recoverable page faults
S - Total number of CPU seconds used by the system in kernel mode
U - Total number of CPU seconds used by user mode
W - Number of times the process was swapped out of main memory
X - Average amount of shared text in the process
Z - System's page size in kilobytes
c - Number of times the process was context-switched
e - Elapsed real time used by the process in seconds
k - Number of signals delivered to the process
p - Average unshared stack size of the process in kilobytes
r - Number of socket messages received by the process
s - Number of socket messages sent by the process
t - Average resident set size of the process in kilobytes
w - Number of time the process was context-switched voluntarily
x - Exit status of the command
We can use the formatting switches as follows:
$ time -f "Elapsed Time = %E, Inputs %I, Outputs %O"
The output for the above command would be something like this:
Elapsed Time = 0:01:00, Inputs 2, Outputs 1
If we want to add a new line as part of the format string use the newline character as follows:
$ time -f "Elapsed Time = %E \n Inputs %I \n Outputs %O"
Linux Time Command Versions
There are three-time command Versions, Bash, Zsh and Gnu time command. We can use the
type command to determine whether time is a binary or a built-in keyword.
$ type time
output # Bash time is a shell keyword # Zsh time is a reserved word # GNU time (sh) time is /usr/bin/time
Please write your suggestions or comments on time command and for more information use time man pages.