Linux time Command | Explained

Time management in Linux is a key parameter for executing commands and managing the system’s performance. It shows the time of the system, which indicates the system’s speed and time taken by a specific command. The time command runs the programs and summarizes the time for the system resource usage. This guide will give a better understanding of this command using the following topics:

Let’s start the guide!

Now let’s start discussing every point in detail from the next section

What is the time Command in Linux?

The time command is used to get the real execution time, the user CPU processing time, and the system CPU processing time. This command tells us the time it will be taken for a command to execute, which helps test the performance of the commands and scripts.

How to install Kali Linux in Window...

The General Syntax of the time command is as follows:

$ time [options] <command>

The components of the syntax are described below:

  • time: Indicates the time command.
  • option: Replace with built-in options of time command.
  • command: Write your desired command for pausing.

Options for time Command

The time command has several built-in options which can be utilized by using the options with the command. The available options are written below:

-oThis option overwrites the file’s content and destroys the file’s previous content.
-aAppend the information with the output. This command is used with the “o” option.
-fFormats the output for time command.
-pThis option gives us the PSIOX output.
–quietThis option does not show anything in the output.

If you want a detailed idea of the options and syntax for the “time” command, use the below command:

$ man time

Let’s get into the usage of the time command in Linux.

How to Use the time Command in Linux?

We can measure the execution time by prefixing the time with any command. To get the time for the “ls -l” (list the file in a directory) command, use:

$ time ls -l

The output shows three times consumed for running the command real, user, and sys time. Let’s understand these times!

  • Real Time: This time is the time (Wall-clock time) consumed by the system CPU from pressing the “Enter” button to executing the command. The above result shows the real time of 0.014 seconds or 14 milliseconds.
  • User Time: The time in seconds used by the CPU in user mode.
  • System Time: System time is taken by Kernel to process the command.

Similarly, you can get the time taken by a process to copy one file’s content to another.

Example 1: Time Consumed by a Specific Command

To find the time consumed by copying the testfile1.txt to the testfilefile2.txt, execute the below command:

$ time cp testfile1.txt testfile2.txt

Example 2: How to Get POSIX Format Time?

POSIX is a standard format created by IEEE for smooth compatibility between different operating systems. To get the POSIX formatted time for “sudo apt update”, use the following command:

$ time -p sudo apt update

The real execution time is 5.02 seconds, and the system consumed time is 0.02 seconds.

To check the difference between the above two commands (time and time -p), the below output is provided, clearly showing the different formats for both commands:

$ time sudo apt update
$ time -p sudo apt update

Example 3: How to Get All the Time Details for a Command?

The normal time command shows real, user, and system times only, so if you want to display all the time related to the system, use the “/usr/bin/time” directory with the “v” flag of the time command. To get all the times for executing the pwd (print working directory) command, use:

$ /usr/bin/time -v pwd

It can be seen that the user time and system time are 0.00, whereas the elapsed wall clock shows the time taken by this to execute the command.

Example 4: How to Format the Execution Time in Linux?

By default, the time in Linux is formatted as hhmmss (for example, 2h13m9s), where h represents the hours, m for minutes, and s for seconds. Usually, the executions do not take hours to complete, so that you would see the time format as 3m2.16s.

To format the time in Linux, we use the /usr/bin/time file, where all the time-related command utilities are present. Let’s check the options for formatting the time Linux:

%Shows a command literal
%ETo show the real-time
%UFor user
%SFor system
%CComand line arguments and name.
%IFile inputs for the process.
%PPercentage CPU, which completes this process.
%rSocket messages received by the system.
xExit the command.

Let’s get into the details of these options.

Format the Time Using the /usr/bin/time Command

The directory /usr/bin/time has details about the time in Linux. To use the time command according to a specific format, we can use the /usr/bin/time directory as follows:

$ /usr/bin/time [options] <Command>

Let’s discuss the different working examples for this command.

Example 4.1: How to Format the Real, User, and System Time?

We can format the Real-time, User time, and System time using the /usr/bin/time file. If you want to show all the built-in times on the output for the “sudo apt update” command, use the below command utility:

$ /usr/bin/time -f "%E real,%U user,%S sys" sudo apt update
  • f: Shows the format option.
  • %E: Represents Real-time
  • %U: Represents User time
  • %S: Represents System time

The output shows the formatted time output with all three system times.

Example 4.2: How to Format the Real Time Only?

If you are running a command and require the Real-time only, you can format it by just using the “%E” option. For instance: to get only the real-time for the “sudo apt update”, execute the following command:

$ /usr/bin/time -f "%E real" sudo apt update

Example 4.3: How to Format the User Consumed (CPU) Time?

While executing the command, the time utilized by the User CPU (outside the Kernel) is User time.  To get the User time only for “sudo apt update”, run the below command:

$ /usr/bin/time -f "%U User" sudo apt update

Example 4.4: How to Save the Execution Time of a Command in a File?

Sometimes, we need to save the execution times of the system to keep the performance record of the system. The /usr/bin/time allows us to put the time in a file named testfile3, using the below command:

Note: The “o” flag of /usr/bin/time command outputs the time into a file rather than the terminal.

$ /usr/bin/time -o testfile3.txt sudo apt update

End of this informative guide!

Conclusion

Linux time command is used to get the execution time, including real-time consumed by the system, user mode time, and system mode time. The time can be formatted to multiple formats using the available options in the time command and /usr/bin/time command. This blog post has covered the working and usage of the time command in Linux.