FactorPad
Faster Learning Tutorials

Linux time command summary with examples

time allows you to show command execution time.
  1. Purpose - Learn what time is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with time.
  4. Script - Add time to our script and run it.
  5. A tip - Finish off with one more insight.
Paul Alan Davis, CFA, November 14, 2016
Updated: August 6, 2018
In this tutorial, 88 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Next

~/ home  / tech  / linux essentials  / time command


The Linux time command

Intermediate

Learn to measure execution time at the Linux command line.

Video Tutorial

Linux time command summary with examples (3:38)

Videos can also be accessed from the YouTube Playlist.

Video Script

The Command and Why You Need It

Our eighty-eighth word, or command to memorize is time from our category System.

time allows you to show command execution time.

Recall from our last video (tutorial) we started a script to tie together what we've learned, and now we'll measure the elapsed time it takes to run our newly created script using a shell built-in.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The time command has 1 option and the argument is a command, and since we're writing it, we're calling it a script.

Unlike most commands, help is not available with double-dash --help, so here look in the man page for bash, as you would with other built-ins.

So why is time an important command? Well, help it's helpful to measure our script run time. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. So to get ahead of any confusion, I'm referring to the shell built-in here for the time command. Many Linux distributions have a different time program. To see what you have use a type -a from video #11, like so.

$ type -a time time is a shell keyword

You can see, mine is just the built-in.

Now, let's open the script from video (tutorial) #87, using video #20's nano in the ~/bin/ directory and called funscript.

$ nano ~/bin/funscript

And add video #43's sleep command so the script takes 2 seconds.

GNU nano 2.2.6 File: /home/factorpad/bin/funscript #!/bin/bash # The shebang gives the absolute path to the executable program # To see if it points to the right executable try: which bash # This is our first script called funscript # From video 87, print text and the current date/time echo -e "\nThe current date and time:" date # From video 88, we measure the time to run the script sleep 2s [ Read 14 lines ] ^G Get Help ^0 WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos ^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Text ^T To Spell

Remember to add a comment, and feel free to pause the video to catch up on your end if you'd like. Then close Ctrl-x and save y.

Next, let's time this script, using time funscript.

$ time funscript The current date and time: Mon Nov 14 15:02:04 PST 2016 real 0m2.028s user 0m0.000s sys 0m0.000s

First is the time from start to finish, then the user CPU time and system CPU time.

And last, to clarify, we didn't have to source the script like we did in the last video (tutorial) #87 as our PATH now includes this ~/bin directory, so bash can find it.

A Final Tip

Okay now you know how to use time. And you know the syntax for commands, options and arguments.

One last tip about the time command. So for added functionality, try installing and aliasing a more full-featured time command on your end if you'd like.

Okay, thanks for visiting today. I hope this was a helpful introduction to the time command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

If you need a break from shell scripting, check out the other free stuff our YouTube Channel has to offer. Subscribe here.

  • For the Outline to all 100 tutorials, click Outline.
  • To go back to the source command, hit Back.
  • To learn the next shell scripting topic read, click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / time command



 
 
Keywords:
linux time command
linux timer
time server linux
execution time linux
linux timer example
linux speedtest
command line speed
linux execution speed
unix time
command execution speed
bash time
bash script timing
man time
shell script timing
shell scripting
linux time tutorial