如何将输出附加到文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5342832/
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-04 00:31:29  来源:igfitidea点击:

How to append the output to a file?

linuxshellcommand-line

提问by Tom Brito

How can I do something like command > filein a way that it appends to the file, instead of overwriting?

我怎样才能command > file以将其附加到文件而不是覆盖的方式进行操作?

采纳答案by Mike Lewis

Use >>to append:

使用>>附加:

command >> file

回答by EdvardM

Yeah.

是的。

command >> fileto redirect just stdoutof command.

command >> file只是重定向标准输出command

command >> file 2>&1to redirect stdoutand stderrto the file (works in bash, zsh)

command >> file 2>&1stdoutstderr重定向到文件(在 bash、zsh 中工作)

And if you need to use sudo, remember that just

如果您需要使用sudo,请记住

sudo command >> /file/requiring/sudo/privilegesdoes not work, as privilege elevation applies to commandbut not shell redirection part. However, simply using teesolves the problem:

sudo command >> /file/requiring/sudo/privileges不起作用,因为权限提升适用于command但不适用于 shell 重定向部分。但是,只需使用即可 tee解决问题:

command | sudo tee -a /file/requiring/sudo/privileges

command | sudo tee -a /file/requiring/sudo/privileges

回答by user3680358

you can append the file with >> sign. It insert the contents at the last of the file which we are using.e.g if file let its name is myfile contains xyz then cat >> myfile abc ctrl d

您可以使用 >> 符号附加文件。它在我们正在使用的文件的最后插入内容。例如,如果文件让其名称为 myfile 包含 xyz 然后 cat >> myfile abc ctrl d

after the above process the myfile contains xyzabc.

在上述过程之后,myfile 包含 xyzabc。