How to redirect the output of the time command to a file in Linux?
Simple. The GNU time
utility has an option for that.
But you have to ensure that you are not using your shell's builtin time
command, at least the bash
builtin does not provide that option! That's why you need to give the full path of the time
utility:
/usr/bin/time -o time.txt sleep 1
Wrap time
and the command you are timing in a set of brackets.
For example, the following times ls
and writes the result of ls
and the results of the timing into outfile
:
$ (time ls) > outfile 2>&1
Or, if you'd like to separate the output of the command from the captured output from time
:
$ (time ls) > ls_results 2> time_results