Home Learn Linux Top 10 uses of the ‘time’ command in Linux

Top 10 uses of the ‘time’ command in Linux

The 'time' command in Linux provides invaluable insights into the duration and resource utilization of processes. Explore our top 10 uses to enhance your workflow and gain a deeper understanding of your system's performance.

by Arun Kumar
time command linux

We’ve all been there. You run a command in Linux, and while it’s executing, you wonder, “How long is this going to take?” or “How much resources is it using?” Enter the time command, one of my personal favorites when I’m working in the Linux environment. It’s simple, yet powerful in its utility.

Throughout this article, I’ll share the top 10 ways you can use the time command, enhancing your command-line productivity and understanding. But before that, let’s take a quick look at the command itself.

Time command in Linux

The time command in Linux is used to measure the execution time of a command or script. It prints a summary of real-time, user CPU time, and system CPU time spent by executing a command when it terminates.

To use the time command, simply type time followed by the command or script you want to measure. Don’t worry, I will cover everything that you should know as a Linux user. Let’s get started.

Top 10 uses of Linux ‘time’ command

Here is a summary of commands for your quick reference.

No. Description Syntax
1. Basic Usage of the ‘time’ Command time <command>
2. Time a Script Execution time ./script.sh
3. Use with ‘find’ Command time find / -name "*.log"
4. Pipe Execution Time `time command1
5. Time Command Iterations time for i in {1..10}; do command; done
6. Using the ‘verbose’ Mode /usr/bin/time -v command
7. Time with Custom Output Format /usr/bin/time -f "Time taken: %E" command
8. Redirecting Output to a File /usr/bin/time -o output.txt command
9. Time with Multiple Commands time (command1; command2)
10. Comparing Command Execution Times Use time with each command to compare

Let’s go into the details of each of these commands.

1. Basic usage of the ‘time’ command

Syntax: time <command>

The most straightforward use of the time command is just to prefix it to any other Linux command. This will give you a summary of how long that command took to run.

Output:

$ time ls
file1.txt file2.txt
real    0m0.002s
user    0m0.001s
sys     0m0.001s

2. Time a script execution

Syntax: time ./script.sh

Being a script enthusiast, I often use time to measure how long my scripts take to run. This is especially helpful when optimizing or debugging scripts.

Output:

$ time ./fosslinux.sh
Script output here...
real    0m10.045s
user    0m5.002s
sys     0m3.042s

3. Use with ‘find’ command

Syntax: time find / -name "*.log"

I admit, I’m impatient when it comes to long-running commands, especially find. By using time, I can understand if it’s the search taking long or just a file system delay.

Output:

$ time find / -name "error.log"
/home/user/error.log
/var/logs/error.log
real    0m3.324s
user    0m0.011s
sys     0m0.213s

4. Pipe execution time

Syntax: time command1 | command2

Although I’m not a big fan of overly complex pipe commands, sometimes they’re unavoidable. And knowing how long the entire pipe chain takes can be insightful.

Output:

$ time cat largefile.txt | grep "error"
Error line here...
real    0m2.012s
user    0m1.903s
sys     0m0.109s

5. Time command iterations

Syntax: time for i in {1..10}; do command; done

When running commands in loops, especially for performance tests, it’s crucial to know the total time taken. I find this especially handy when benchmarking.

Output:

$ time for i in {1..5}; do echo "iteration $i"; done
iteration 1
iteration 2
iteration 3
iteration 4
iteration 5
real    0m0.005s
user    0m0.002s
sys     0m0.003s

6. Using the ‘verbose’ mode

Syntax: /usr/bin/time -v command

This one’s a gem! The verbose mode provides a lot more information than the default output. It includes details on memory usage, context switches, and more.

Output:

$ /usr/bin/time -v ls
Command being timed: "ls"
User time (seconds): 0.00
System time (seconds): 0.00
Percent of CPU this job got: 40%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00
...

7. Time with custom output format

Syntax: /usr/bin/time -f "Time taken: %E" command

I won’t lie, I’ve got a soft spot for customization, and with time, you can change the output format to display the information in a way you prefer.

Output:

$ /usr/bin/time -f "Time taken: %E" ls
file1.txt file2.txt
Time taken: 0:00.00

8. Redirecting output to a file

Syntax: /usr/bin/time -o output.txt command

When I’m documenting or need to share my results, redirecting time output to a file is a lifesaver. This way, I keep the terminal clean and store the results for later reference.

Output:

$ /usr/bin/time -o timeoutput.txt ls
(file contents of timeoutput.txt will have the time data)

9. Time with multiple commands

Syntax: time (command1; command2)

Another trick up my sleeve is timing multiple commands together. By wrapping commands in parentheses, you can measure the cumulative time.

Output:

$ time (echo "First"; echo "Second")
First
Second
real    0m0.002s
user    0m0.001s
sys     0m0.001s

10. Comparing command execution times

A personal practice of mine is to compare times of two different commands that achieve the same result. This is an informal benchmarking tactic.

Sample execution:

$ time grep "pattern" largefile.txt
$ time awk '/pattern/' largefile.txt

Conclusion

The ‘time’ command in Linux may initially appear insignificant, but it becomes invaluable with frequent use. Regardless of whether you are a beginner or an experienced system administrator, understanding command and script performance can offer valuable insights and optimizations. And, there’s a certain joy in knowing precisely how long your commands take to execute, right? So, the next time you’re on the terminal, give the ‘time’ command a try!

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.