如何将输出追加到文本文件末尾

37 浏览
0 Comments

如何将输出追加到文本文件末尾

我该如何将命令的输出附加到文本文件的末尾?

admin 更改状态以发布 2023年5月20日
0
0 Comments

使用>>来追加一个文件

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  

然后输出为

hello tom

0
0 Comments

当将输出重定向到文件时,使用>>代替>

your_command >> file_to_append_to

如果file_to_append_to不存在,它将被创建。

示例:

$ echo "hello" > file
$ echo "world" >> file
$ cat file 
hello
world

0