Linux 如何将文本附加到文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17701989/
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
How do I append text to a file?
提问by Dchris
What is the easiest way to append text to a file in Linux?
在 Linux 中将文本附加到文件的最简单方法是什么?
I had a look at this question, but the accepted answer uses an additional program (sed
) I'm sure there should be an easier way with echo
or similar.
我看过这个问题,但接受的答案使用了一个额外的程序 ( sed
) 我相信应该有一个更简单的方法echo
或类似的方法。
采纳答案by Jon Kiparsky
cat >> filename
This is text, perhaps pasted in from some other source.
Or else entered at the keyboard, doesn't matter.
^D
Essentially, you can dump any text you want into the file. CTRL-D sends an end-of-file signal, which terminates input and returns you to the shell.
本质上,您可以将想要的任何文本转储到文件中。CTRL-D 发送一个文件结束信号,它终止输入并将您返回到 shell。
回答by Cyclonecode
How about:
怎么样:
echo "hello" >> <filename>
Using the >>
operator will append data at the end of the file, while using the >
will overwrite the contents of the file if already existing.
使用>>
运算符将在文件末尾追加数据,而使用>
将覆盖文件内容(如果已存在)。
You could also use printf
in the same way:
您也可以printf
以相同的方式使用:
printf "hello" >> <filename>
Note that it can be dangerous to use the above. For instance if you already have a file and you need to append data to the end of the file and you forget to add the last >
all data in the file will be destroyed. You can change this behavior by setting the noclobber
variable in your .bashrc
:
请注意,使用上述内容可能很危险。例如,如果您已经有一个文件并且您需要将数据附加到文件的末尾而您忘记添加最后一个>
文件中的所有数据都将被破坏。您可以通过noclobber
在您的 中设置变量来更改此行为.bashrc
:
set -o noclobber
Now when you try to do echo "hello" > file.txt
you will get a warning saying cannot overwrite existing file
.
现在,当您尝试这样做时,echo "hello" > file.txt
您会收到一条警告说cannot overwrite existing file
。
To force writing to the file you must now use the special syntax:
要强制写入文件,您现在必须使用特殊语法:
echo "hello" >| <filename>
You should also know that by default echo
adds a trailing new-line character which can be suppressed by using the -n
flag:
您还应该知道,默认情况下echo
会添加一个尾随换行符,可以使用以下-n
标志来抑制它:
echo -n "hello" >> <filename>
References
参考
回答by user9869932
Other possible way is:
其他可能的方法是:
echo "text" | tee -a filename >/dev/null
The -a
will append at the end of the file.
在-a
将附加在文件的结尾。
If needing sudo
, use:
如果需要sudo
,请使用:
echo "text" | sudo tee -a filename >/dev/null
回答by user12345
Follow up to accepted answer.
跟进接受的答案。
You need something other than CTRL-D to designate the end if using this in a script. Try this instead:
如果在脚本中使用它,您需要CTRL-D以外的其他东西来指定结束。试试这个:
cat << EOF >> filename
This is text entered via the keyboard or via a script.
EOF
This will append text to the stated file (not including "EOF").
这会将文本附加到指定的文件(不包括“EOF”)。
It utilizes a here document (or heredoc).
However if you need sudo to append to the stated file, you will run into trouble utilizing a heredoc due to I/O redirection if you're typing directly on the command line.
但是,如果您需要 sudo 附加到指定的文件,如果您直接在命令行上输入,则由于 I/O 重定向,您将在使用 heredoc 时遇到麻烦。
This variation will work when you are typing directly on the command line:
当您直接在命令行上键入时,此变体将起作用:
sudo sh -c 'cat << EOF >> filename
This is text entered via the keyboard.
EOF'
Or you can use tee
instead to avoid the command line sudo issue seen when using the heredoc with cat:
或者,您可以改用tee
使用带有 cat 的 heredoc 时出现的命令行 sudo 问题:
tee -a filename << EOF
This is text entered via the keyboard or via a script.
EOF