如何将命令的输出附加到文本文件的末尾?
当前回答
当指向文件输出时,使用>>而不是>:
your_command >> file_to_append_to
如果file_to_append_to不存在,则创建该文件。
例子:
$ echo "hello" > file
$ echo "world" >> file
$ cat file
hello
world
其他回答
当指向文件输出时,使用>>而不是>:
your_command >> file_to_append_to
如果file_to_append_to不存在,则创建该文件。
例子:
$ echo "hello" > file
$ echo "world" >> file
$ cat file
hello
world
要追加一个文件,请使用>>
echo "hello world" >> read.txt
cat read.txt
echo "hello siva" >> read.txt
cat read.txt
那么输出应该是
hello world # from 1st echo command
hello world # from 2nd echo command
hello siva
要覆盖文件,请使用>
echo "hello tom" > read.txt
cat read.txt
那么输出是
你好,汤姆
在Linux操作系统中,可以使用cat命令将文件内容追加到另一个文件
cat fileName_1.txt >> fileName_2.txt
在前面的命令中,您将把fileName_1.txt的内容追加到fileName_2.txt。
在Windows操作系统中可以使用type命令
type fileName_1.txt >> fileName_2.txt
请看这张gif图片:
使用tee和选项-a(——append)允许您一次追加到多个文件,也可以使用sudo(在追加到受保护的文件时非常有用)。除此之外,如果您需要使用bash之外的其他shell,这很有趣,因为并非所有shell都支持>和>>操作符
echo "hello world" | sudo tee -a output.txt
这个帖子有关于tee的很好的答案
使用>>操作符将文本附加到文件中。