There are times you’ll want to know how long it took for a command or script to execute. 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 by leveraging the time
command.
The command-line is full of a bunch of small, single-purpose utilities that can help eliminate the need to write any additional code yourself. The time
command is one such command. The time
command is in no way related to the date
command, which provides the system’s date and time. Instead, it times a program or script’s execution and tells you how long it took.
In this tutorial, you’ll use the time
command and explore its output.
The examples in this tutorial (unless otherwise noted) will be using time
that’s built into the Bash shell on Linux.
To time the execution duration of a command, you prefix your command with time
.
However, how you execute time
depends on your operating system. time
exists both as a built-in command for some shells, like bash
and zsh
, as well as a stand-alone command, known as GNU time
which has different arguments than the time
built into shells.
Use the following command to see how time
works on your system:
- type -a time
You’ll see output like the following:
Outputtime is a shell keyword
time is /usr/bin/time
In this case, there’s both a built-in shell command called time
, and a version of time
installed at /usr/bin/time
.
If you want to use the GNU version of time
instead, you can prefix it with a backslash:
- \time
If you don’t, your shell will use its built-in version instead.
Note: the fish
shell doesn’t provide its own implementation of time
. If you happen to be using fish
, you will want to make sure you have the GNU time
command installed.
Both methods serve the same purpose, although the GNU version has more advanced formatting options.
Let’s explore timing program execution by using the tree
command on the root /
of your file system, which will list out a visual tree of all of your files and directories.
The tree
command is not always installed by default on many systems, but you can install it on Ubuntu and Debian systems with apt
:
- sudo apt install tree
On macOS, you can install tree
with Homebrew:
- brew install tree
Now that the tree
command is installed, use it to look at all the files on the system, but prefix it with time
to see how long it takes:
- time tree /
You’ll see the file information scroll through. Eventually, it’ll stop and present you with the time taken:
Output# The output of tree will scroll by here.
166590 directories, 1568127 files
tree / 12.24s user 10.37s system 66% cpu 33.827 total
Notice the command you executed, tree /
, is echoed back by the time
command as well. The output shows a few pieces of info, but for now, focus on the total
and the number just prior:
Outputtree / 12.24s user 10.37s system 66% cpu 33.827 total
That’s how long the command took to execute in seconds.
The time
command also works if you happen to cancel a command with CTRL+C
. If you were to run time tree /
again and quickly hit CTRL+C
, the tree
command will stop scrolling by, and you’ll be presented with the time
results for the time the command was executing before you stopped it.
The output from time
includes three values, in addition to the total duration.
Outputtree / 12.24s user 10.37s system 66% cpu 33.827 total
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
.
The difference between user
and system
time is that 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 its isolation, crashes are always recoverable.
Kernel mode, on the other hand, is when code being executed has unrestricted access to the system hardware. 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.
In this tutorial, you explored how to use the time
command to see how long commands and scripts take to run, and where that time goes. Your shell’s time
command gives you quick access to timing the execution duration of a command without any additional scripting.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.