118

In UNIX/LINUX, is there an easy way to track the time a command takes?

OneChillDude
  • 7,856
  • 10
  • 40
  • 79

4 Answers4

165

Yes, use time <command>, such as

time ls

Consult man time for more options. Link.

Paolo
  • 20,112
  • 21
  • 72
  • 113
squiguy
  • 32,370
  • 6
  • 56
  • 63
  • 4
    And, the meaning of real/user/sys times is nicely covered [here](http://stackoverflow.com/questions/556405/what-do-real-user-and-sys-mean-in-the-output-of-time1) – prideout Nov 14 '14 at 17:16
  • This answer is inaccurate for bash users on linux. The manpage documents the Gnu time command, but time is a builtin in bash, which doesn't have all the options documented there. –  Jun 17 '17 at 15:27
6

Use

/usr/bin/time 

instead that the time builtin in the bash: it is more configurable AFAIK.

e.g. /usr/bin/time --format=' \n---- \nelapsed time is %e'ls
Paolinux
  • 167
  • 1
  • 8
  • As far as I can tell, this is default. This was the case on the CentOS 6, CentOS 7 and Debian 8 systems I checked: `user@host:~$ which time /usr/bin/time` Looks to be version 1.7 of GNU time. – Toby Apr 07 '17 at 16:11
  • 4
    @Toby: Even though "which" says it's /usr/bin/time, in bash, the builtin overrides that. If I do `time -f "\t%E real" ls` in bash, I get an error, but it works if I do `/usr/bin/time -f "\t%E real" ls`. –  Jun 17 '17 at 15:24
  • 1
    You're right. That's very interesting and enlightening. Thanks! – Toby Jun 18 '17 at 23:11
  • 1
    Don't use `which`. Use `type -a`: `$ which time /usr/bin/time $ type -a time time is a shell keyword time is /usr/bin/time` – Daniel-Dane Mar 13 '18 at 09:27
  • Note that using `/usr/bin/time` prevents you from using bash aliases. The bash builtin `time` is needed for that, else you'll get the error `cannot run my_alias: No such file or directory`. – Jamie S Sep 20 '18 at 17:28
6

Here is how a sleep of one second looks like, timed with time:

$ time sleep 1

real    0m1.001s
user    0m0.000s
sys 0m0.000s
Guillaume Chevalier
  • 9,613
  • 8
  • 51
  • 79
0

The command time is built-in in the bash but it can also be installed on most distros by installing the package "time" (apt install time) and must be accessed by doing /usr/bin/time.

Using /usr/bin/time offers more convenient options like specifying a format:

time --format="Duration: %e seconds" sleep 3
Jarchiii
  • 336
  • 3
  • 5