Command-line Basics: Timing Command Execution


The command-line is full of a bunch of small, single purpose utilities that can help eliminate the need of writing any additional code yourself. Sure, you could track the start and end times and calculate the difference to get the duration. You could even grab your trusty stopwatch [app] and track things that way. Or, you could save yourself all that trouble and simply leverage time.

Getting started

For a such a simple command, time is actually a bit confusing, conceptionally.

First off, the time command is in no way related to the date command which provides the system’s date and time.

Next, time exists both as a built-in for some shells, like bash and zsh as well as a stand-alone command, known as GNU time.

If you were to type in man time, you will most likely be presented with the man page for the stand-alone command, which features a bunch of different arguments than the time built into shells actually supports.

To make matters worse, the fish shell doesn’t seem to provide it’s own implementation of time. If happen to be using fish, you will want to make sure you have the time command installed.

And to take it all a step further, if you’re using bash or zsh and have the GNU version of time installed, you have to reference it with a backslash, \time, else your shell will use it’s built-in version instead.

At their core though, they both can serve the same purpose, with the GNU version allowing more advanced formatting options.

With all that said, both “versions” of time are non-destructive in nature, and the example commands we’ll be timing below will also be non-destructive as well. If so desired, time can very well be used with destructive commands like rm.

The examples below (unless otherwise noted) will be using time that’s built into my favorite shell, and the new default shell on macOS, zsh.

Timing command execution

Timing the execution duration of a command is as easy as prefixing your command (any command) with time. A great command for trying this out is the tree command on the root / of your file system, which will list out a visual tree of all of your files and directories:

$ time tree /

# Long dramatic pause while a bunch of text scrolls on by...

166590 directories, 1568127 files
tree /  12.24s user 10.37s system 66% cpu 33.827 total

Notice the command we executed, tree / is the echoed back by the time command. We’ll skip over the next stuff for right now, and focus just on total and the number just prior. That’s how long the command took to execute, in seconds.

The time command, also works if you happen to bail on a command with CTRL-C. If we were to run time tree / again and quickly hit CTRL-C the tree will stop scrolling by and we’ll be presented with the time results for the time the command was executing before we killed it.

Try it out!

Tracking resource usage

The output from time includes 3 values, in addition to the total duration. The first is the total amount of time (in CPU-seconds) that the command spent in user mode. That’s the value with user after it.

The next, suffixed by system is the amount of time (again, in CPU-seconds) that the command spent in system, or kernel mode. Finally, the percentage of the CPU that was allocated to the command, suffixed with cpu.

What the heck are user and kernel modes anyway?

Glad you asked! CPU usage is broken down by access levels.

When code is executed in user mode, it doesn’t have direct access to hardware or reference memory and must rely on APIs of the system for delegation. This is how most code runs on your system, and due to it’s isolation, crashes are always recoverable.

Kernel mode on the other hand, is when code being executed has unrestricted access to the system hardware. Think of it like the most super user access you can have on a system.

This mode is pretty much reserved for the most trusted functions of the operating system. Because of having complete access, when things crash in kernel mode, they crash bad and tend to take the system down with them.


I had originally planned to call this post time and time again, but as you can see, the time that’s built into your command-line shell is fairly limited and doesn’t really “time again”.

Even though it’s extremely limited in comparison to it’s stand-alone GNU brethren, your shell’s time still allows you quick and easy access to timing the execution duration of a command without any additional scripting!

  Tweet It

🕵 Search Results

🔎 Searching...