Linux Basics: Understanding the time Command for New Users

Linux is a huge topic, with lots to learn… but that’s no problem because the Linux Crash Course series will teach you valuable Linux-related concepts and skills – one video at a time. In this episode, Jay covers the time command.

YouTube player

Basic Usage

The time command is very easy to learn, basically all we have to do is use the time command with another command.

time sudo apt dist-upgrade

When we execute it, it tells us how long it took to run the process. And you can place any command or script you want after the time command, the example I gave is specific to Debian-based distributions so you can replace it with something else if you want.

But already, we can see how useful this command can be. If I’m curious how long it’s taking to update my system, I can time it.

On my end, I use time with my Ansible provisioning system runs. The very last thing that’s printed is the output from the time command, so every time my systems provision I can see how long it took.

One time I noticed the time it took for provision jobs to finish doubled – which isn’t normal. The culprit ended up being an error that caused Ansible to run twice each time. The time command made the problem obvious, and I was able to fix it.

Anyway, let’s understand the output a bit more. For the most part, you’ll probably be interested in real time, which starts ticking as soon as you execute the command.

User time refers to user mode, as opposed to kernel mode. System time is where we see how long the task ran in kernel mode. These different modes are beyond the scope of this article, so we’ll focus on real time for now.

An option that might be useful to add to the time command is -o. This option enables you to write the output to a file.

/usr/bin/time -o results.txt sudo apt dist-upgrade

However, not all systems will have that option with the time command. But it really doesn’t matter anyway, since we can just redirect the output ourselves:

time sudo apt dist-upgrade > results.txt

Anyway, as you can see, the time command is literally that simple – just type time, followed by another command. I think this is one of those commands that is only as useful as your imagination. There’s all kinds of different use-cases for it.