How Do I Time a Specific Command?

In Linux, the “time” is a command-line tool that allows users to measure the amount of time it takes for a specific command to run. It is beneficial for optimizing the system’s performance or identifying slow-running commands. It gets a more detailed picture of how long a command takes to run and where it is spending its resources.

This guide will briefly explain the “time” command and how users can time a specific command in Linux:

How Do I Time a Specific Command?

To time a specific command in Linux, use the “time”  by specifying the particular command. The syntax for using the “time” command is as follows:

$ time [options] command

Here, “options” are optional arguments that can be used to customize the output of the “time” command, and “command” is the command that users want to time.

If users want to customize the output of the “time” command, use the following options:

  • “-p“: Displays the output in a more portable format.
  • -f format“: Specify a custom format for the output.
  • -o file“: Redirects the output to a file.

The “time” command measures the execution time of a command in three different ways:

  • Real-time: The actual elapsed time it takes for the command to run (time spent waiting for I/O or other processes).
  • User time: The time spent from the CPU in user mode.
  • System time: The CPU time spent in system mode.

There are different ways to use the “time” command in Linux to time a specific command. 

Here are some examples:

Example 1: Time a Specific Command

An example is carried out to measure the command time it takes to run. For this, type the “time” command followed by the command the user wants to time. In our case, find out the time of the “ls” command via the following script:

$ time ls

The output of the “time” command displays the amount of real-time (0m0.016s), user time (0m0.004s), and system time (0m0.005s) it took for the command to run.

Example 2: Time a Specific Command in a Portable Format (Easily Understandable)

To find out the time and display the output in a portable format that is easily understandable, use the time command with the “p” option by specifying the particular command as “$ sudo apt update” in the below script:

$ time -p sudo apt update

The above command executions display the output in a portable format using the “p” option.

Example 3: Time a Specific Command Using Pipeline 

Users can also time a pipeline of commands by enclosing them in parentheses via the “time” command. For instance, sorts and counts the number of unique lines in a “file.txt” through the below command:

$ time (sort file.txt | uniq -c)

It displays the time of both the “sort” and “uniq” commands together and shows the total time it took for the pipeline to run.

Note: Users can also utilize the “/usr/bin/time” command instead of the “time” command to perform different operations.

Example 4: Human-Readable Format 

To customize the output of the “/usr/bin/time” command, use the “-f” option. For instance, show only the real-time and memory usage of a command via the below script:

$ /usr/bin/time -f "%E real" ls

It shows the real-time “0:00.02” in a more human-readable format.

Example 5: Save Output to a File 

Users can save the output of the “/usr/bin/time” command to a file using the “-o” option. For this, save the output of the “time” command to a file called “file.txt“:

$ /usr/bin/time -o file.txt ls

It saves the output of the “time” command to the “file.txt” file on the screen.

Conclusion

To time a specific command in Linux, use the “time<command>” script. It shows users how long the command took to run. Users can also utilize the “p” and “f” options to customize the output of the “time” command in a portable, human-readable format. Also, users can save the output to a file through the “o” option. This article has explained how users can time a specific command with different examples in Linux.