如何将linux时间命令的结果重定向到某个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11082233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:55:48  来源:igfitidea点击:

how to redirect result of linux time command to some file

linux

提问by Julias

I'm running the following command (on Ubuntu)

我正在运行以下命令(在 Ubuntu 上)

time wget 'http://localhost:8080/upLoading.jsp' --timeout=0

and get a result in the command line

并在命令行中得到结果

real    0m0.042s
user    0m0.000s
sys     0m0.000s

I've tried the following:

我尝试了以下方法:

time -a o.txt wget 'http://localhost:8080/upLoading.jsp' --timeout=0 

and get the following error

并得到以下错误

-a: command not found

I want to get the result to be redirected to some file. How can I do that?

我想将结果重定向到某个文件。我怎样才能做到这一点?

采纳答案by Levon

You can direct the stdoutoutput of any commmand to a file using the >character. To appendthe output to a file use >>

您可以stdout使用该>字符将任何命令的输出定向到文件。要将输出附加到文件使用>>

Note that unless done explicitly, output to stderrwill still go to the console. To direct bothstderrand stdoutto the same output stream use

请注意,除非明确完成,否则输出 tostderr仍将进入控制台。为了指导stderrstdout相同的输出流使用

   command 2>&1 outfile.txt (with bash)

or

或者

   command >& outfile.txt (with t/csh)

If you are working with bashAll about redirectionwill give you more details and control about redirection.

如果您正在使用bashAll about redirection将为您提供有关重定向的更多详细信息和控制。

回答by Ribtoks

Exactly the timefrom GNU writes it's output to stderrand if you want to redirect it to file, you can use --output=PATH parameter of time

正是time来自 GNU 将其输出写入stderr,如果您想将其重定向到文件,您可以使用 --output=PATH 参数time

See this http://unixhelp.ed.ac.uk/CGI/man-cgi?time

看到这个http://unixhelp.ed.ac.uk/CGI/man-cgi?time

And if you want to redirect stdoutto some file, you can use > filenameto create file and fill it or >> filenameto append to some file after the initial command.

如果您想将标准输出重定向到某个文件,您可以使用> filename来创建文件并填充它或>> filename在初始命令之后附加到某个文件。

If you want to redirect stderrby yourself, you can use $ command >&2 your_stderr_output

如果你想自己重定向stderr,你可以使用$ command >&2 your_stderr_output

回答by claasz

Checking man time, I guess what you need is

检查man time,我想你需要的是

time -o o.txt -a ...

(Note you need both-a and -o).

(请注意,你需要两个-a和-o)。

[EDIT:] If you are in bash, you must also take care to write

[编辑:] 如果你在 bash,你也必须小心写

/usr/bin/time

(check manpage for explanation)

(查看手册页以获取解释)

回答by Zagorax

You can do that with >if you want to redirect the output.

>如果你想重定向输出,你可以这样做。

For example:

例如:

time wget 'http://localhost:8080/upLoading.jsp' --timeout=0 > output.txt 2>&1

2>&1says to redirect STDERR to the same file.

2>&1说将 STDERR 重定向到同一个文件。

This command will erase any output.txt files and creates a new one with your output. If you use >>it will append the output at the end of any existing output.txt file. If it doesn't exist, it will create it.

此命令将删除所有 output.txt 文件并使用您的输出创建一个新文件。如果您使用>>它,它将在任何现有 output.txt 文件的末尾附加输出。如果它不存在,它将创建它。

回答by Adam

-a is only understood by the time binary (/usr/bin/time), When just using time you're using the bash built-in version which does not process the -a option, and hence tries to run it as a command.

-a 只能被时间二进制文件 (/usr/bin/time) 理解,当只使用 time 时,您使用的是不处理 -a 选项的 bash 内置版本,因此尝试将其作为命令运行.

/usr/bin/time -o foo.txt -a wget 'http://localhost:8080/upLoading.jsp' --timeout=0

回答by dwalter

Try to use /usr/bin/timesince many shells have their own implementation of timewhich may or may not support the same flags as /usr/bin/time

尝试使用,/usr/bin/time因为许多 shell 都有自己的实现,time它们可能支持也可能不支持与以下相同的标志/usr/bin/time

so change your command to

所以改变你的命令

  /usr/bin/time -a -o foo.txt wget ....

回答by ctrl-alt-delor

\time 2> time.out.text command

\time -o time.out.text command

This answer based on earlier comments. It is tested it works. The advantage of the \over /usr/bin/is that you don't have to know the install directory of time.

这个答案基于之前的评论。经测试有效。\over的好处/usr/bin/是你不必知道time.

These answers also only capture the time, not other output.

这些答案也只捕获时间,而不是其他输出。

回答by y.tk

How about your LANG ?

你的 LANG 怎么样?

$ time -ao o.txt echo 1
bash: -ao: コマンドが見つかりません

real    0m0.001s
user    0m0.000s
sys     0m0.000s
$ export|grep LANG
declare -x LANG="ja_JP.utf8"

$ LANG=C time -ao o.txt echo 1
1

$ cat o.txt
0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 1984maxresident)k
0inputs+0outputs (0major+158minor)pagefaults 0swaps

回答by emery

Try:

尝试:

command 2> log.txt

and the real-time output from "command" can be seen in another console window with:

并且“命令”的实时输出可以在另一个控制台窗口中看到:

tail -f log.txt