Understanding the time Command in Linux

Filed Under: UNIX/Linux
TIME COMMAND

The time command in Linux outputs how long it takes for a command to run. It is used along with other commands to analyze the time taken to execute.

The time command comes in handy to measure the performance of scripts. Time taken is an important metric to measure performance.

Using the time command

Using the time command in Linux is as simple as writing ‘time’ before the command you want to execute. Let’s see how long it takes to display the output of the ls command:

$ time ls
Time Ls

We get the output which consists of three different types of times. We’ll get to them in a bit.

Let’s try another command :

$ time ping linuxfordevices.com
Time Ping

The output of the time command comes after the output of the command we are running it with.

The three types of times in the end are real, user and sys.

  • Real: This is the time taken from when the call was given till the point the call is completed. This is the time that has passed when measured in real-time.
  • User: This is the amount of CPU time spent in user mode.
  • Sys: This is the amount of CPU time spent in kernel mode. Sys is short for system time.

Versions of the time command in Linux

There are different versions of time command. Three to be specific. These are :

  • Bash
  • Zsh
  • GNU time (sh)

You can see which one you are using with :

$ type time

According to the version you are using, you’ll get the following output:

BASH

 time is a shell keyword

ZSH

 time is a reserved word

GNU time

  time is /usr/bin/time
Type Of Time

Bash and Zsh have their own built-in versions of the time command which take precedence over the GNU time command if they’re installed. To use time from GNU we’ll have to explicitly mention it. We’ll see this next.

1. GNU time command (Default)

To access the time command from GNU use:

$ /usr/bin/time
Time GNU

Time command from GNU gives a more detailed output.

$ /usr/bin/time ls
Time GNU
Time GNU

One benefit of GNU time command is that it’s easy to play around with. To display output in a verbose manner, use the option -v with the time command.

$ /usr/bin/time -v ls
Time Command in Linux - Verbose output

The GNU time command lets you format the output and store it in a file. We’ll see this next.

2. Storing the output to a file

To save the output to a file, use the -o option with GNU time command. The command to do this is:

$ /usr/bin/time -o time_output.txt ls
Time command in Linux - Output to a file

The command will create a file by the name of time_output.txt and then store the output in it.

To append the output at the end of an existing file use the command :

$ /usr/bin/time -a /home/smart/time-output.txt ls

3. Formatting of GNU time output

Another variation available for the GNU time command is that it can be formatted and represented in a more friendly manner. To format the output -f flag is used along with time command.

GNU time command provides a host of resource specifiers that can be used in the command.

To get the list of resource specifiers use the command :

$ man /usr/bin/time
GNU Time Resource Specifiers

The way to use this is :

$ /usr/bin/time -f "\t%C [Command name],\t%K [Total memory usage],\t%E [Time Elapsed]"  ping journaldev.com

\t adds a tab between the output.

Format GNU Output

Conclusion

We saw the use of time command and its default version of the command in action. Using time command to measure the performance of commands/ scripts is very common. To learn more about time command refer to its man page.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages