How To Use time command in Linux

Wondering how to use the time command in Linux?

Working on a Linux system means you’ll find yourself making use of the Command Terminal to execute a series of processes on your system. This method can be tedious if you need to execute a process multiple times, as writing the same commands again and again could get redundant and time-consuming.

To counter this, most users make use of scripts. A script is a file that contains a list of commands that need to be executed sequentially in the order they’re given. This can save a lot of time as it gets rid of the process of repeatedly typing commands in the Terminal.

One of the methods to check the efficiency of a script is to see how much time it takes for it to execute a certain process. For this purpose, you can make use of the time command. If you’re unfamiliar with the time command and wish to master it, then we’ll help you have a clear understanding of how to use the time command in Linux.

So without further ado, let’s begin!

Understanding time in Linux.

Before you proceed to learn about the time command, it’s recommended that you have an idea of what “time” refers to in Linux.

Time can be classified into three categories:

  1. Real-Time: This refers to the time taken by a script to execute. It is measured in terms of the “real” world i.e. standard wall clock time.
  2. User-Time: This is the time taken for computation. The unit of measurement is CPU cycles per second.
  3. System-Time: This is the total time taken to execute all processes during the execution of your script or program. It is also measured in terms of CPU cycles per second.

Now that you’re aware of what time refers to in Linux, you can proceed to learn how to use the time command in Linux.

The time Command.

The time command is used to display the time taken for a task to be executed. This can be real, user, or system time based on user preference.

The syntax for the time command is as follow:

$ time <options> <command/script name>

You can find out more details about the time command by typing the following in the Command Terminal:

$ help time
time command in Linux

This will display a help window with details about the command.

The time command also has three different types based on the version of the shell that is being used. You can type the following in the Command Terminal to find the type of your shell:

$ type time
type

The output should be one of the following

  1. “Time is a shell keyword.” This indicates that you’re using the $bash version of the command.
  2. “Time is a reserved word.” This implies that you’re using the $Zsh version of the command.
  3. “Time is /usr/bin/Time.” This means that you’re using the $GNU version of the command.

With the basics covered, you are now ready to learn how to use time command in Linux.

Implementing the time Command.

There are many ways to use the time command.

The command can be used to find out the time taken for the execution of a command or script. 

Follow these steps to use the time command:

1. Start by opening the Command Terminal. You can achieve this by pressing Ctrl + Shift + T on your keyboard.

2. Next, type the following:

$ time <command>

In this example, we’ll use time for the $ping command i.e:

$ time timedatectl

The output should resemble the content shown in the image below:

sample output

It should be noted that the output may differ because of the type of shell that you’re using.

Exporting Output of the time Command.

You can also export the output of the time command to an external text file. 

To achieve this, follow these steps:

1. First, open the Command Terminal by pressing Ctrl + Alt + T on your keyboard.

2. Next, type the following command:

$ /usr/bin/time -o /home/ubuntu/T-output.txt timedatectl
exporting the output

This should export the output of the Time command to the newly created “T-output.txt” file.

You can check the contents of the T-output.txt file by typing the following in the Command Terminal:

$ cat T-output.txt
time command in linux


Getting Detailed Output Using the time Command.

The time command can also be used to produce detailed output for a command. 

This can be achieved with the help of the -v option.

Follow these steps to get the detailed output using the time command.

1. Start by opening the Command Terminal. You can achieve this by pressing Ctrl + Shift + T on your keyboard.

2. Next, type the following:

$ /usr/bin/time -v <command>

In our case:

$ /usr/bin/time -v timedatectl

This will display a detailed version of the Time output on the Command Terminal as shown in the image below.

time command in linux


Customizing Output of the time Command.

The time command can be changed to display only a certain amount of data.

This can be achieved using the format or -f option.

Follow these steps to customize the time command output.

1. First, open the Command Terminal by pressing Ctrl + Alt + T on your keyboard.

2. Next, type the following command:

$ /usr/bin/time -f “<parameters>” <command>

For example:

$ /usr/bin/time -f "\t%C [Command details],\t%k [Number of signals process received]" timedatectl

While on the topic of time, it should be noted that having the correct time on your system is necessary to receive updates. Errors can result because of the system clock not being in sync with the world clock. To make sure your system clock is always in sync with the world clock, you can make use of timezones.

Selecting the correct timezone is a simple process that will save you from encountering many errors when updating your repositories.

We hope this guide helped you learn how to use the time command in Linux. If you have any questions, or suggestions through which we can make our guides better, let us know in the comment section down below.

If this guide helped you, please share it. 🙂

Leave a Reply

Your email address will not be published.

Previous Post

How to Use curl command in Linux

Next Post

How to Refund Subscription from Bumble

Related Posts