17

Is there a way to time a series of commands in linux?

Perhaps something like this:

/usr/bin/time { ls; pwd; ls }
  • even without the time command this would fail as it needs the final ; i.e. { ls ; pwd ; ls ; } – northern-bradley Nov 3 '18 at 19:07
9

You could try to wrap it in a shell command:

/usr/bin/time /bin/sh -c 'ls;pwd;ls'
| improve this answer | |
31

Using time use () to create a subshell for the commands you wish to time. The syntax would be:

time ( ls; pwd; ls )

If using /usr/bin/time then add the commands to a shell script (for example my.sh) and time the shell script-:

/usr/bin/time my.sh
| improve this answer | |
  • 2
    This works with time but not with /usr/bin/time. Running the command you gave produces -bash: syntax error near unexpected token ls'` – juniper- Jun 17 '13 at 12:32
  • Although the accepted answer at unix.stackexchange.com/a/46106/419658 works great, I still prefer this version because it makes the code much readable, instead of just being passed as a string to sh -c. Anyway, Can somebody please point a situation when one would be preferred over another? – Bhavesh Diwan Aug 13 at 1:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.