Command Line

Time A Command To See How Long It Takes To Run

Have you ever wanted to time a command? Well, you can! Linux includes the ability to time a command, so that you can know how long it took for a command to be processed. 

Today, we’ll be using the ‘time‘ command. Time is simply (and accurately) described in the man page as:

time – run programs and summarize system resource usage

The time command is a pretty nifty tool and the output will tell you the real time that it took, as well as the system time, to run the command you’re timing. There’s really not a whole lot more to say about the time command. It does what it says on the tin. It times stuff!

This is going to just be a quick article and should be easy to follow. There’s not a whole lot to explain and it’s pretty straightforward. If you’re looking to get your feet wet playing in the terminal, this is probably a good article to start with.

Time A Command:

Just like oh so many of these articles, you’ll need an open terminal. So, let’s crack open your default terminal by pressing CTRL + ALT + T on your keyboard.

Now, let’s take the simple command to list everything in a directory:

ls -la

To find out how long it took to list all the files and folders in a directory, you could use:

time ls -la

The output of that command tells you how long it took to list all the items in a directory beneath the results. It should not have taken long. If you want to try something bigger, something that lasts longer, you can take a look at this command:

for i in {0..99999}; do echo "I love Linux-Tips!"; done

That should take a just a little more time to run in your terminal, but how much longer? Well, you can can actually see how long it really took by adding ‘time’ in front of it. Obviously, it’d look something like this:

time for i in {0..99999}; do echo "I love Linux-Tips!"; done

The output at the end is something like this:

real 0m0.566s
user 0m0.423s
sys 0m0.143s

The ‘real‘ is how much time it really took. The ‘user‘ is how much time it took for the user. The ‘sys‘ is how much time it took for the system – the amount of time that the kernel actually devoted to running that command.

You can time all sorts of stuff. Like, here’s an example output from me updating my system with time sudo apt update (not all of us have big, fat pipes from the ISP):

real 0m2.677s
user 0m2.276s
sys 0m0.522s

When I purged Krita (from a previous article), the output from time sudo apt purge krita looks like:

real 0m8.469s
user 0m1.943s
sys 0m0.406s

Have fun with it, if you want. Find out how long those tasks actually are, so that you can get a reasonable estimate of where you’re spending your time in the terminal. You can even use it like time nano <file_name> and see how long it took you to edit a file in the terminal!

Like I said, have fun with it. You might find some of those tasks that seem to go slow really don’t take all that much time. Maybe you’ll find out that the reverse is true? One thing is pretty certain, however. If you didn’t have it before, you now have ‘time’ as a new tool in your Linux toolbox.

Closure:

Well, there’s another article. This one has taught you how to time a command in the Linux terminal. It’s a little tool, probably not all that useful, but it’s one that’s there. If you happen to use the time command on a regular basis, please leave a comment letting us know why. I’m sure there’s some real-world uses, but other than looking for bottlenecks or real-time optimizations I can’t really think of any.

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

KGIII

Retired mathematician, residing in the mountains of Maine. I may be old and wise, but I am not infallible. Please point out any errors. And, as always, thanks again for reading.

Recent Posts

Enable/Disable Your Network Interface

Today we'll cover one way to enable or disable your network interface in the Linux…

7 months ago

Check Your NIC Speed In The Terminal

Today's exercise is a nice and simple exercise where we check your NIC speed in…

7 months ago

Easily Monitor Your Wireless Connection

Have you ever wanted to easily monitor your wireless connection? Well, now you can learn…

7 months ago

Count The Files In A Directory

I think I've covered this before with the ls command but this time we'll count…

7 months ago

Get System Information With The ‘uname’ Command In Linux

Today we'll be learning about a basic Linux command that's known as 'uname' and it…

7 months ago

hardinfo Has Been Rebooted As hardinfo2

If you've used hardinfo in the past, it may interest you to know that hardinfo…

7 months ago