如何在Linux中将time命令的输出重定向到文件?

如何在Linux中将time命令的输出重定向到文件?

问题描述:

关于在Linux上计时程序的一个小问题:time命令允许 测量程序的执行时间:

Just a little question about timing programs on Linux: the time command allows to measure the execution time of a program:

[ed@lbox200 ~]$ time sleep 1

real    0m1.004s
user    0m0.000s
sys     0m0.004s

哪个工作正常.但是,如果我尝试将输出重定向到文件,它将失败.

Which works fine. But if I try to redirect the output to a file, it fails.

[ed@lbox200 ~]$ time sleep 1 > time.txt

real    0m1.004s
user    0m0.001s
sys     0m0.004s

[ed@lbox200 ~]$ cat time.txt 
[ed@lbox200 ~]$ 

我知道还有其他一些实现时间的选项-o来写文件,但是 我的问题是关于没有这些选项的命令.

I know there are other implementations of time with the option -o to write a file but my question is about the command without those options.

有什么建议吗?

尝试

{ time sleep 1 ; } 2> time.txt

将时间"的STDERR和您的命令组合到time.txt中.

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

或使用

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

将休眠"中的STDERR放入文件"sleep.stderr"中,只有时间"中的STDERR放入"time.txt"中

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