This is not an answer to the OP's question. The OP would like to store only the output of the time
part of the command into the file, not the stderr
output of all of the subcommands also into the file. My approach below, however, will optionally store all output into the file, including the stderr
of all subcommands. That's what I want, not what the OP wants.
My answer is therefore only an answer to the title of the question, which is:
How to run time on multiple commands AND write the time output to file?
How to time a multiline, multi-subcommand command in bash, and optionally store all output into a file
ie: How to time multiple sub-commands and the totality of the whole group of commands using parenthesis (( )
) in bash, and optionally store the entire output into a file.
To build a bit more on @Martin.T's answer, here is the format with parenthesis that I like to use when I have a long and complicated multi-line command. It kind of reminds me of Python a bit, because it is prettier to look at than most bash I might see.
Running time
on the individual sub-commands is optional. Use ;
or &&
as you see fit after each subcommand.
;
is used after a command to indicate "run the next command no matter what (ex: even if this one fails)", and
&&
is used after a command to indicate: "only run the next command if this one passes".
- The backslashes (
\
) after each line indicate that the command is a multi-line command and continues on to the next line. For a multi-line command, you must have a backslash after each line except the last one.
Example general format:
# Option 1: only run the command
time ( \
time command1; \
time command2 && \
time command3 && \
time command4 \
)
To also store the entire output into a file named output.txt
, do this instead. Description:
- The extra set of outer parenthesis captures the output from the first, outer-most bash built-in
time
command as well,
- the
2>&1
part redirect stderr
(file descriptor 2
) to stdin
(file descriptor 1
), since time
outputs its results to stderr
otherwise, and
- piping to the
tee
command with | tee output.txt
causes the results, which are now on stdout
, to both be displayed to the screen and to be written to the output.txt
file.
# Option 2: run the command _and_ store the results into an `output.txt` file
(time ( \
time command1; \
time command2 && \
time command3 && \
time command4 \
)) 2>&1 | tee output.txt
Example demonstration commands:
# Option 1: only run the command
time ( \
time sleep 1; \
time sleep 2 && \
time sleep 3 && \
time sleep 1 \
)
# Option 2: run the command _and_ store the results into an `output.txt` file
(time ( \
time sleep 1; \
time sleep 2 && \
time sleep 3 && \
time sleep 1 \
)) 2>&1 | tee output.txt
Example demo command and output:
Notice that you can see the time of each subcommand as 1 sec, 2 sec, 3 sec, 1 sec, followed by the total time of 7 sec:
$ time ( \
> time sleep 1; \
> time sleep 2 && \
> time sleep 3 && \
> time sleep 1 \
> )
real 0m1.013s
user 0m0.002s
sys 0m0.000s
real 0m2.001s
user 0m0.001s
sys 0m0.000s
real 0m3.001s
user 0m0.001s
sys 0m0.000s
real 0m1.001s
user 0m0.000s
sys 0m0.001s
real 0m7.017s
user 0m0.005s
sys 0m0.001s
If you run the "Option 2" command above, you'll also see this exact same output above now stored into the just-created output.txt
file. View the contents of that file with:
cat output.txt
Follow-up question
I'm trying to figure out a little bit more about storing the output into a file in a more granular way, like the OP wants, but using the technique I've posted above. I've posted this follow-up question of mine here: How to store inner output of a nested bash command (within parenthesis) into one file and outer output into another file?