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:
To find out how long it took to list all the files and folders in a directory, you could use:
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:
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:
The output at the end is something like this:
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):
When I purged Krita (from a previous article), the output from
time sudo apt purge krita looks like:
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 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.
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.