如何将文本追加到文件中?

时间:2022-11-05 13:57:48

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或类似的东西。

4 个解决方案

#1


80  

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。

#2


109  

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:

注意,使用上述方法可能是危险的。例如,如果您已经有一个文件,并且需要将数据附加到文件的末尾,并且忘记添加最后的>,那么文件中的所有数据都将被销毁。您可以通过在.bashrc中设置noclobber变量来更改此行为:

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”>文件时。你会得到一个警告说不能覆盖现有文件。

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

引用

#3


17  

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

#4


8  

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:

如果在脚本中使用此命令,则需要其他东西来指定结束。试试这个:

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来避免在使用heredoc和cat时遇到的命令行sudo问题:

tee -a filename << EOF
This is text entered via the keyboard or via a script.
EOF

#1


80  

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。

#2


109  

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:

注意,使用上述方法可能是危险的。例如,如果您已经有一个文件,并且需要将数据附加到文件的末尾,并且忘记添加最后的>,那么文件中的所有数据都将被销毁。您可以通过在.bashrc中设置noclobber变量来更改此行为:

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”>文件时。你会得到一个警告说不能覆盖现有文件。

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

引用

#3


17  

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

#4


8  

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:

如果在脚本中使用此命令,则需要其他东西来指定结束。试试这个:

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来避免在使用heredoc和cat时遇到的命令行sudo问题:

tee -a filename << EOF
This is text entered via the keyboard or via a script.
EOF