How to Use The time Command in Linux

Time Command

The time command in Linux is used to determine how long a given command will take to run. We use the time command to test the performance of scripts and commands.

What’s the Need for the time Command in Linux?

If you’re a system administrator working on a Linux system, it is necessary for you to create scripts and cron jobs that can automate manual repetitive tasks. But depending on the complexity of the script, the server loads can be higher. And to top that up, if you’re running multiple scripts of similar complexity simultaneously, we’ll have wasted resources on the server.

To solve this problem, we need to ensure that the CPU time taken by any of our automation scripts is as short as possible. This is where the Linux time command is needed.

What Information Does the Linux time Command Display?

The time command displays 3 different sets of times for the specified script:

  • Real Time – The actual time that has passed from the execution to the end of the script
  • User Time – The time spent by CPU in user mode
  • System Time – The CPU time utilized

1. Different Linux time Command Versions

Depending on the shell that you’re using, the time command version could be completely different and functionality that we describe here may not be available by default. The three different versions for the time command in Linux that you might come across are:

  • Bash shell time command – Accessible directly typing time in a bash shell
  • Zsh shell time command – Can be used directly by typing time in a zsh shell
  • GNU Default Linux time command – This will be available by typing in /usr/bin/time

The basic output to execute the run time for any command is available in all the three time commands in Linux. But a few of the formatting and command options are available with GNU time. You can use the alias command in Linux to set up the alias for GNU time.

I’ve already set up the alias for the GNU time in my system. Use the below command to create an alias:

alias time=/usr/bin/time

You can identify the version of the command that you’re using with the use of the type command.

type time

Depending on the time command version that you’re using you’ll get one of the below outputs.

# For bash shell
time is a shell keyword

# For Zsh shell
time is a reserved word

# The GNU time 
time is /usr/bin/time

2. Measure Execution Time for Other Commands

Now that you have a basic understanding of what the command is, let’s get into the usage. The use cases can range from timing scripts for performance, to timing other commands. The time command in Linux will simply output the time since execution to the exit irrespective of what’s being run.

Let’s ping Google and check the time that’s required for the same.

time ping -c 2
Time Command Default
Time Command Default

We pinged Google only two times for this demonstration. The actual time elapsed was 1 second while the user and CPU times were negligible.

3. Get Detailed Statistics of Command Runtime

You can get detailed statistics of any command instead of only the default information that’s visible by default. The -v option allows you to

time -v ping -c 2
Linux time command in Linux
Time Verbose Output

Great, now you can see much more detailed information about the command runtime.

4. Formatting the Output of the Linux time Command

The GNU Time command in Linux provides us with multiple options to format and display the runtime. The -f option paired with the formatting options that can be found on the man page can provide us with output that’s as per our needs.

man time

Scroll down and you’ll find the formatting options. Here’s a screenshot of the options.

Man Time Formatting Screenshot
Man Time Formatting Screenshot

We’ll use a few of those for this demonstration and you can continue to explore the time command outputs further by reading through the man page.

time -f "Elapsed time: %E" ping -c 2
Time Command Formatting
Time Command Formatting

As you can see the last line now shows the elapsed time, with the text “Elapsed time:” as we expected. Play around with the formatting options to get the required information printed.

5. Saving the Linux time Command Output to a File

We may not always be looking at the terminal screen and some commands or scripts that we test could take a longer amount of time. We can redirect the output to a file for viewing and transferring the information around.

time -o time-command.log ping -c 2

This will save the time required for the script to run in a file with the name that you specify.

Time Output File
Time Output File

I saved the file as time-command.log but you can save it with any other name. To demonstrate, I’ve displayed the contents of the file using cat command.


In this tutorial, we covered the time command in Linux and understood the command in good detail. We hope you now have a good understanding of time command in Linux.