Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect the output of the time command to a file in Linux?

Tags:

linux

bash

time

People also ask

How do I redirect output of a command in Unix?

Option One: Redirect Output to a File Only To use bash redirection, you run a command, specify the > or >> operator, and then provide the path of a file you want the output redirected to. > redirects the output of a command to a file, replacing the existing contents of the file.

How do I redirect the contents of a file in Linux?

In Linux, for redirecting output to a file, utilize the ”>” and ”>>” redirection operators or the top command. Redirection allows you to save or redirect the output of a command in another file on your system. You can use it to save the outputs and use them later for different purposes.

How do you redirect the output of ls command in a file?

to redirect the output of the ls command into a file called "ls. out" in your home directory. Remember that the tilde (~) is Unix shorthand for your home directory. In this command, the ls command will list the contents of the /tmp directory.

Which characters will redirect the output of a command in Linux?

The pipe character redirects the standard output from one command to the standard input of another command.


Try

{ time sleep 1 ; } 2> time.txt

which combines the STDERR of "time" and your command into time.txt

Or use

{ time sleep 1 2> sleep.stderr ; } 2> time.txt

which puts STDERR from "sleep" into the file "sleep.stderr" and only STDERR from "time" goes into "time.txt"


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

If you care about the command's error output you can separate them like this while still using the built-in time command.

{ time your_command 2> command.err ; } 2> time.log

or

{ time your_command 2>1 ; } 2> time.log

As you see the command's errors go to a file (since stderr is used for time).

Unfortunately you can't send it to another handle (like 3>&2) since that will not exist anymore outside the {...}

That said, if you can use GNU time, just do what @Tim Ludwinski said.

\time -o time.log command