如何将 time 命令的输出重定向到 Linux 中的文件? [英] How to redirect the output of the time command to a file in Linux?

查看:35
本文介绍了如何将 time 命令的输出重定向到 Linux 中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于 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

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

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

这篇关于如何将 time 命令的输出重定向到 Linux 中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆