Connect with us

Website

Timing Your Shell Commands Using The Time Command In Linux

Published

on

Timing Your Shell Commands Using The Time Command In Linux

Have you ever thought it would be nice to time a command? Do you end up taking out a stopwatch or your smartphone? I have good news… Linux has a command for that, the time command. The time command in Linux gives you the option to time command from the very shell you run the command from.

What Is The Linux Time Command?

Have you ever thought it would be nice to time a command? Do you end up taking out a stopwatch or your smartphone? The time command in Linux is used to measure command execution time from the very shell you run the command from.

How Do I Get The Time Command?

On most Linux distributions the time command is pre-installed. If you find it isn’t preinstalled try searching for the package by the name of your package manager. If neither of these works please refer to your Linux Distributions documentation.

Be aware both Bash and Zsh have built-in versions of the time command. These are both separate from the Upstream GNU version provided by the free software foundation.

Usage

Ok, so you have a command you want to time, for example, it could be that a command is taking a while, or could be as simple as wanting to have numbers on how long a command takes.

To measure execution time for a command replace command in the following command:

Advertisement
time command

Note: The GNU version of the time command has many flags that can be used but is not universal compared to other versions of the time command. If you find yourself using GNU time be aware it is not universal and refer to the man page for flags.

Understanding the output

Below is the output you will get from the basic syntax.

real 0m0.000s

user 0m0.000s

sys 0m0.000s

The descriptions below explain what is being measured for each.

Advertisement

real – The Real-time is the execution time of the command from start to finish.

user – The user time is the CPU time spent executing system time.

sys – The system time is the time spent on system calls and the like.

Examples

Time the time it takes to recursively copy from one directory to another

time cp -r /source/directory /destination/directory

Time a script

Advertisement
time ./script.sh

Time and output the results of the ls command to a file

time ls > output.txt 2>&1

Time with a shell pipe

time (command1 | command2)

Time a MySQL query

time mysql -u username -p -e "SELECT * FROM table_name"

Conclusion

The time command is rather simple but simple isn’t a bad thing, as even the simplest Linux commands can be priceless. In this blog post, we have discussed what the time command is, that it should be available on almost all Linux distributions by default, how to use the command, and how to understand the resulting output. Finally ending on some examples of how you can use the time command with various other commands.

Advertisement

Stephen Oduntan is the founder and CEO of SirsteveHQ, one of the fastest growing independent web hosts in Nigeria. Stephen has been working online since 2010 and has over a decade experience in Internet Entrepreneurship.

Continue Reading
Advertisement
Comments

Trending

Copyright © 2024 SirsteveHQ. All Rights Reserved.