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:
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.
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
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.
Additional Links
Want to read more about other Linux Commands check out our other posts.
Embracing a lifelong passion for technology since childhood, CJ delved into the intricate workings of systems, captivated by the desire to understand the unknown. This innate curiosity led to his discovery of Linux, a revelation that resonated deeply. With more than 7 years of on the job experience, he’s honed his technical skills as a Geek and Senior Linux Systems Administrator.
Leave a Reply